clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

IncludeInserter.cpp
Go to the documentation of this file.
1 //===-------- IncludeInserter.cpp - clang-tidy ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "IncludeInserter.h"
11
12 namespace clang {
13 namespace tidy {
14
15 class IncludeInserterCallback : public PPCallbacks {
16 public:
17 explicit IncludeInserterCallback(IncludeInserter *Inserter)
18 : Inserter(Inserter) {}
19 // Implements PPCallbacks::InclusionDerective(). Records the names and source
20 // locations of the inclusions in the main source file being processed.
21 void InclusionDirective(SourceLocation HashLocation,
22 const Token & /*include_token*/,
23 StringRef FileNameRef, bool IsAngled,
24 CharSourceRange FileNameRange,
25 const FileEntry * /*IncludedFile*/,
26 StringRef /*SearchPath*/, StringRef /*RelativePath*/,
27 const Module * /*ImportedModule*/) override {
28 Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
29 FileNameRange.getEnd());
30 }
31
32 private:
33 IncludeInserter *Inserter;
34 };
35
36 IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
37 const LangOptions &LangOpts,
38 IncludeSorter::IncludeStyle Style)
39 : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
40
41 IncludeInserter::~IncludeInserter() {}
42
43 std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
44 return llvm::make_unique<IncludeInserterCallback>(this);
45 }
46
47 llvm::Optional<FixItHint>
48 IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
49 bool IsAngled) {
50 // We assume the same Header will never be included both angled and not
51 // angled.
52 if (!InsertedHeaders[FileID].insert(Header).second)
53 return llvm::None;
54
55 if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
56 // This may happen if there have been no preprocessor directives in this
57 // file.
58 IncludeSorterByFile.insert(std::make_pair(
59 FileID,
60 llvm::make_unique<IncludeSorter>(
61 &SourceMgr, &LangOpts, FileID,
62 SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)),
63 Style)));
64 }
65 return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
66 }
67
68 void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled,
69 SourceLocation HashLocation,
70 SourceLocation end_location) {
71 FileID FileID = SourceMgr.getFileID(HashLocation);
72 if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
73 IncludeSorterByFile.insert(std::make_pair(
74 FileID, llvm::make_unique<IncludeSorter>(
75 &SourceMgr, &LangOpts, FileID,
76 SourceMgr.getFilename(HashLocation), Style)));
77 }
78 IncludeSorterByFile[FileID]->AddInclude(file_name, IsAngled, HashLocation,
79 end_location);
80 }
81
82 } // namespace tidy
83 } // namespace clang
LangOpts
LangOptions LangOpts
Definition: ClangTidy.cpp:168
clang::tidy::IncludeInserterCallback::InclusionDirective
void InclusionDirective(SourceLocation HashLocation, const Token &, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, const FileEntry *, StringRef, StringRef, const Module *) override
Definition: IncludeInserter.cpp:21
clang::tidy::IncludeSorter::IncludeStyle
IncludeStyle
Definition: IncludeSorter.h:26
clang::tidy::IncludeInserter::IncludeInserter
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
Definition: IncludeInserter.cpp:36
clang::tidy::IncludeInserter::CreateIncludeInsertion
llvm::Optional< FixItHint > CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled)
Definition: IncludeInserter.cpp:48
SourceMgr
SourceManager SourceMgr
Definition: ClangTidy.cpp:172
clang::tidy::IncludeInserterCallback::IncludeInserterCallback
IncludeInserterCallback(IncludeInserter *Inserter)
Definition: IncludeInserter.cpp:17
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:37
PPCallbacks
clang::tidy::IncludeInserter::~IncludeInserter
~IncludeInserter()
Definition: IncludeInserter.cpp:41
clang::tidy::IncludeInserter
Definition: IncludeInserter.h:47
clang::tidy::IncludeInserterCallback
Definition: IncludeInserter.cpp:15
clang::tidy::IncludeInserter::CreatePPCallbacks
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Definition: IncludeInserter.cpp:43
IncludeInserter.h