Debian Codegraph @

llvm-project/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h

raw
1 //===--- Record.h - Record compiler events ------------------------- C++-*-===//
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 // Where Analysis.h analyzes AST nodes and recorded preprocessor events, this
10 // file defines ways to capture AST and preprocessor information from a parse.
11 //
12 // These are the simplest way to connect include-cleaner logic to the parser,
13 // but other ways are possible (for example clangd records includes separately).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef CLANG_INCLUDE_CLEANER_RECORD_H
18 #define CLANG_INCLUDE_CLEANER_RECORD_H
19
20 #include "clang-include-cleaner/Types.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/FileSystem/UniqueID.h"
27 #include <memory>
28 #include <vector>
29
30 namespace clang {
31 class ASTConsumer;
32 class ASTContext;
33 class CompilerInstance;
34 class Decl;
35 class FileEntry;
36 class Preprocessor;
37 class PPCallbacks;
38 class FileManager;
39
40 namespace include_cleaner {
41
42 /// Captures #include mapping information. It analyses IWYU Pragma comments and
43 /// other use-instead-like mechanisms (#pragma include_instead) on included
44 /// files.
45 ///
46 /// This is a low-level piece being used in the "Location => Header" analysis
47 /// step to determine the final public header rather than the header directly
48 /// defines the symbol.
49 class PragmaIncludes {
50 public:
51 /// Installs an analysing PPCallback and CommentHandler and populates results
52 /// to the structure.
53 void record(const CompilerInstance &CI);
54
55 /// Returns true if the given #include of the main-file should never be
56 /// removed.
57 bool shouldKeep(unsigned HashLineNumber) const {
58 return ShouldKeep.find(HashLineNumber) != ShouldKeep.end();
59 }
60
61 /// Returns the public mapping include for the given physical header file.
62 /// Returns "" if there is none.
63 llvm::StringRef getPublic(const FileEntry *File) const;
64
65 /// Returns all direct exporter headers for the given header file.
66 /// Returns empty if there is none.
67 llvm::SmallVector<const FileEntry *> getExporters(const FileEntry *File,
68 FileManager &FM) const;
69 llvm::SmallVector<const FileEntry *> getExporters(tooling::stdlib::Header,
70 FileManager &FM) const;
71
72 /// Returns true if the given file is a self-contained file.
73 bool isSelfContained(const FileEntry *File) const;
74
75 /// Returns true if the given file is marked with the IWYU private pragma.
76 bool isPrivate(const FileEntry *File) const;
77
78 private:
79 class RecordPragma;
80 /// 1-based Line numbers for the #include directives of the main file that
81 /// should always keep (e.g. has the `IWYU pragma: keep` or `IWYU pragma:
82 /// export` right after).
83 llvm::DenseSet</*LineNumber*/ unsigned> ShouldKeep;
84
85 /// The public header mapping by the IWYU private pragma. For private pragmas
86 // without public mapping an empty StringRef is stored.
87 //
88 // !!NOTE: instead of using a FileEntry* to identify the physical file, we
89 // deliberately use the UniqueID to ensure the result is stable across
90 // FileManagers (for clangd's preamble and main-file builds).
91 llvm::DenseMap<llvm::sys::fs::UniqueID, llvm::StringRef /*VerbatimSpelling*/>
92 IWYUPublic;
93
94 /// A reverse map from the underlying header to its exporter headers.
95 ///
96 /// There's no way to get a FileEntry from a UniqueID, especially when it
97 /// hasn't been opened before. So store the path and convert it to a
98 /// FileEntry by opening the file again through a FileManager.
99 ///
100 /// We don't use RealPathName, as opening the file through a different name
101 /// changes its preferred name. Clearly this is fragile!
102 llvm::DenseMap<llvm::sys::fs::UniqueID,
103 llvm::SmallVector</*FileEntry::getName()*/ llvm::StringRef>>
104 IWYUExportBy;
105 llvm::DenseMap<tooling::stdlib::Header,
106 llvm::SmallVector</*FileEntry::getName()*/ llvm::StringRef>>
107 StdIWYUExportBy;
108
109 /// Contains all non self-contained files detected during the parsing.
110 llvm::DenseSet<llvm::sys::fs::UniqueID> NonSelfContainedFiles;
111
112 /// Owns the strings.
113 llvm::BumpPtrAllocator Arena;
114
115 // FIXME: add support for clang use_instead pragma
116 };
117
118 /// Recorded main-file parser events relevant to include-cleaner.
119 struct RecordedAST {
120 /// The consumer (when installed into clang) tracks declarations in `*this`.
121 std::unique_ptr<ASTConsumer> record();
122
123 ASTContext *Ctx = nullptr;
124 /// The set of declarations written at file scope inside the main file.
125 ///
126 /// These are the roots of the subtrees that should be traversed to find uses.
127 /// (Traversing the TranslationUnitDecl would find uses inside headers!)
128 std::vector<Decl *> Roots;
129 };
130
131 /// Recorded main-file preprocessor events relevant to include-cleaner.
132 ///
133 /// This doesn't include facts that we record globally for the whole TU, even
134 /// when they occur in the main file (e.g. IWYU pragmas).
135 struct RecordedPP {
136 /// The callback (when installed into clang) tracks macros/includes in this.
137 std::unique_ptr<PPCallbacks> record(const Preprocessor &PP);
138
139 /// Describes where macros were used in the main file.
140 std::vector<SymbolReference> MacroReferences;
141
142 /// The include directives seen in the main file.
143 include_cleaner::Includes Includes;
144 };
145
146 } // namespace include_cleaner
147 } // namespace clang
148
149 #endif