clang-tools  10.0.0svn
IncludeInserter.cpp
Go to the documentation of this file.
1 //===-------- IncludeInserter.cpp - clang-tidy ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "IncludeInserter.h"
10 #include "clang/Lex/Token.h"
11 
12 namespace clang {
13 namespace tidy {
14 namespace utils {
15 
17 public:
19  : Inserter(Inserter) {}
20  // Implements PPCallbacks::InclusionDerective(). Records the names and source
21  // locations of the inclusions in the main source file being processed.
22  void InclusionDirective(SourceLocation HashLocation,
23  const Token &IncludeToken, StringRef FileNameRef,
24  bool IsAngled, CharSourceRange FileNameRange,
25  const FileEntry * /*IncludedFile*/,
26  StringRef /*SearchPath*/, StringRef /*RelativePath*/,
27  const Module * /*ImportedModule*/,
28  SrcMgr::CharacteristicKind /*FileType*/) override {
29  Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
30  IncludeToken.getEndLoc());
31  }
32 
33 private:
34  IncludeInserter *Inserter;
35 };
36 
37 IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
38  const LangOptions &LangOpts,
40  : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
41 
43 
44 std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
45  return std::make_unique<IncludeInserterCallback>(this);
46 }
47 
48 llvm::Optional<FixItHint>
49 IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
50  bool IsAngled) {
51  // We assume the same Header will never be included both angled and not
52  // angled.
53  if (!InsertedHeaders[FileID].insert(Header).second)
54  return llvm::None;
55 
56  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
57  // This may happen if there have been no preprocessor directives in this
58  // file.
59  IncludeSorterByFile.insert(std::make_pair(
60  FileID,
61  std::make_unique<IncludeSorter>(
62  &SourceMgr, &LangOpts, FileID,
63  SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)),
64  Style)));
65  }
66  return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
67 }
68 
69 void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
70  SourceLocation HashLocation,
71  SourceLocation EndLocation) {
72  FileID FileID = SourceMgr.getFileID(HashLocation);
73  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
74  IncludeSorterByFile.insert(std::make_pair(
75  FileID, std::make_unique<IncludeSorter>(
76  &SourceMgr, &LangOpts, FileID,
77  SourceMgr.getFilename(HashLocation), Style)));
78  }
79  IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation,
80  EndLocation);
81 }
82 
83 } // namespace utils
84 } // namespace tidy
85 } // namespace clang
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Create PPCallbacks for registration with the compiler&#39;s preprocessor.
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:26
IncludeInserterCallback(IncludeInserter *Inserter)
bool IsAngled
true if this was an include with angle brackets
PathRef FileName
void InclusionDirective(SourceLocation HashLocation, const Token &IncludeToken, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, const FileEntry *, StringRef, StringRef, const Module *, SrcMgr::CharacteristicKind) override
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.