clang-tools  11.0.0git
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 
38  const LangOptions &LangOpts,
40  : SourceMgr(SourceMgr), Style(Style) {}
41 
43 
44 std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
45  return std::make_unique<IncludeInserterCallback>(this);
46 }
47 
48 IncludeSorter &IncludeInserter::getOrCreate(FileID FileID) {
49  // std::unique_ptr is cheap to construct, so force a construction now to save
50  // the lookup needed if we were to insert into the map.
51  std::unique_ptr<IncludeSorter> &Entry = IncludeSorterByFile[FileID];
52  if (!Entry) {
53  // If it wasn't found, Entry will be default constructed to nullptr.
54  Entry = std::make_unique<IncludeSorter>(
55  &SourceMgr, FileID,
56  SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), Style);
57  }
58  return *Entry;
59 }
60 
61 llvm::Optional<FixItHint>
62 IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
63  bool IsAngled) {
64  // We assume the same Header will never be included both angled and not
65  // angled.
66  if (!InsertedHeaders[FileID].insert(std::string(Header)).second)
67  return llvm::None;
68 
69  return getOrCreate(FileID).CreateIncludeInsertion(Header, IsAngled);
70 }
71 
72 void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
73  SourceLocation HashLocation,
74  SourceLocation EndLocation) {
75  FileID FileID = SourceMgr.getFileID(HashLocation);
76  getOrCreate(FileID).AddInclude(FileName, IsAngled, HashLocation, EndLocation);
77 }
78 
79 } // namespace utils
80 } // namespace tidy
81 } // 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
llvm::SourceMgr * SourceMgr
IncludeInserterCallback(IncludeInserter *Inserter)
bool IsAngled
true if this was an include with angle brackets
void AddInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
Adds the given include directive to the sorter.
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.
Optional< FixItHint > CreateIncludeInsertion(StringRef FileName, bool IsAngled)
Creates a quoted inclusion directive in the right sort order.
Class used by IncludeInserterCallback to record the names of the inclusions in a given source file be...
Definition: IncludeSorter.h:23