clang-tools  3.9.0
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 #include "clang/Lex/Token.h"
12 
13 namespace clang {
14 namespace tidy {
15 namespace utils {
16 
18 public:
20  : Inserter(Inserter) {}
21  // Implements PPCallbacks::InclusionDerective(). Records the names and source
22  // locations of the inclusions in the main source file being processed.
23  void InclusionDirective(SourceLocation HashLocation,
24  const Token & IncludeToken,
25  StringRef FileNameRef, bool IsAngled,
26  CharSourceRange FileNameRange,
27  const FileEntry * /*IncludedFile*/,
28  StringRef /*SearchPath*/, StringRef /*RelativePath*/,
29  const Module * /*ImportedModule*/) override {
30  Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
31  IncludeToken.getEndLoc());
32  }
33 
34 private:
35  IncludeInserter *Inserter;
36 };
37 
39  const LangOptions &LangOpts,
41  : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
42 
44 
45 std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
46  return llvm::make_unique<IncludeInserterCallback>(this);
47 }
48 
49 llvm::Optional<FixItHint>
50 IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
51  bool IsAngled) {
52  // We assume the same Header will never be included both angled and not
53  // angled.
54  if (!InsertedHeaders[FileID].insert(Header).second)
55  return llvm::None;
56 
57  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
58  // This may happen if there have been no preprocessor directives in this
59  // file.
60  IncludeSorterByFile.insert(std::make_pair(
61  FileID,
62  llvm::make_unique<IncludeSorter>(
63  &SourceMgr, &LangOpts, FileID,
64  SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)),
65  Style)));
66  }
67  return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
68 }
69 
70 void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
71  SourceLocation HashLocation,
72  SourceLocation EndLocation) {
73  FileID FileID = SourceMgr.getFileID(HashLocation);
74  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
75  IncludeSorterByFile.insert(std::make_pair(
76  FileID, llvm::make_unique<IncludeSorter>(
77  &SourceMgr, &LangOpts, FileID,
78  SourceMgr.getFilename(HashLocation), Style)));
79  }
80  IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation,
81  EndLocation);
82 }
83 
84 } // namespace utils
85 } // namespace tidy
86 } // namespace clang
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Create PPCallbacks for registration with the compiler's preprocessor.
LangOptions LangOpts
Definition: ClangTidy.cpp:189
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:27
IncludeInserterCallback(IncludeInserter *Inserter)
SourceManager SourceMgr
Definition: ClangTidy.cpp:193
bool IsAngled
true if this was an include with angle brackets
void InclusionDirective(SourceLocation HashLocation, const Token &IncludeToken, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, const FileEntry *, StringRef, StringRef, const Module *) override
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
Produces fixes to insert specified includes to source files, if not yet present.
llvm::Optional< FixItHint > CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled)
Creates a Header inclusion directive fixit.