clang-tools  14.0.0git
ExpandModularHeadersPPCallbacks.h
Go to the documentation of this file.
1 //===- ExpandModularHeadersPPCallbacks.h - clang-tidy -----------*- 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 #ifndef LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
10 #define LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
11 
12 #include "clang/Lex/PPCallbacks.h"
13 #include "clang/Lex/Preprocessor.h"
14 #include "llvm/ADT/DenseSet.h"
15 
16 namespace llvm {
17 namespace vfs {
18 class OverlayFileSystem;
19 class InMemoryFileSystem;
20 } // namespace vfs
21 } // namespace llvm
22 
23 namespace clang {
24 class CompilerInstance;
25 
26 namespace serialization {
27 class ModuleFile;
28 } // namespace serialization
29 
30 namespace tooling {
31 
32 /// Handles PPCallbacks and re-runs preprocessing of the whole
33 /// translation unit with modules disabled.
34 ///
35 /// This way it's possible to get PPCallbacks for the whole translation unit
36 /// including the contents of the modular headers and all their transitive
37 /// includes.
38 ///
39 /// This allows existing tools based on PPCallbacks to retain their functionality
40 /// when running with C++ modules enabled. This only works in the backwards
41 /// compatible modules mode, i.e. when code can still be parsed in non-modular
42 /// way.
44 public:
46  CompilerInstance *Compiler,
47  IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS);
49 
50  /// Returns the preprocessor that provides callbacks for the whole
51  /// translation unit, including the main file, textual headers, and modular
52  /// headers.
53  ///
54  /// This preprocessor is separate from the one used by the rest of the
55  /// compiler.
56  Preprocessor *getPreprocessor() const;
57 
58 private:
59  class FileRecorder;
60 
61  void handleModuleFile(serialization::ModuleFile *MF);
62  void parseToLocation(SourceLocation Loc);
63 
64  // Handle PPCallbacks.
65  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
66  SrcMgr::CharacteristicKind FileType,
67  FileID PrevFID) override;
68 
69  void InclusionDirective(SourceLocation DirectiveLoc,
70  const Token &IncludeToken, StringRef IncludedFilename,
71  bool IsAngled, CharSourceRange FilenameRange,
72  const FileEntry *IncludedFile, StringRef SearchPath,
73  StringRef RelativePath, const Module *Imported,
74  SrcMgr::CharacteristicKind FileType) override;
75 
76  void EndOfMainFile() override;
77 
78  // Handle all other callbacks.
79  // Just parse to the corresponding location to generate PPCallbacks for the
80  // corresponding range
81  void Ident(SourceLocation Loc, StringRef) override;
82  void PragmaDirective(SourceLocation Loc, PragmaIntroducerKind) override;
83  void PragmaComment(SourceLocation Loc, const IdentifierInfo *,
84  StringRef) override;
85  void PragmaDetectMismatch(SourceLocation Loc, StringRef, StringRef) override;
86  void PragmaDebug(SourceLocation Loc, StringRef) override;
87  void PragmaMessage(SourceLocation Loc, StringRef, PragmaMessageKind,
88  StringRef) override;
89  void PragmaDiagnosticPush(SourceLocation Loc, StringRef) override;
90  void PragmaDiagnosticPop(SourceLocation Loc, StringRef) override;
91  void PragmaDiagnostic(SourceLocation Loc, StringRef, diag::Severity,
92  StringRef) override;
93  void HasInclude(SourceLocation Loc, StringRef, bool, Optional<FileEntryRef> ,
94  SrcMgr::CharacteristicKind) override;
95  void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *,
96  SourceLocation StateLoc, unsigned) override;
97  void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier,
98  ArrayRef<int>) override;
99  void PragmaWarningPush(SourceLocation Loc, int) override;
100  void PragmaWarningPop(SourceLocation Loc) override;
101  void PragmaAssumeNonNullBegin(SourceLocation Loc) override;
102  void PragmaAssumeNonNullEnd(SourceLocation Loc) override;
103  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &,
104  SourceRange Range, const MacroArgs *) override;
105  void MacroDefined(const Token &MacroNameTok,
106  const MacroDirective *MD) override;
107  void MacroUndefined(const Token &, const MacroDefinition &,
108  const MacroDirective *Undef) override;
109  void Defined(const Token &MacroNameTok, const MacroDefinition &,
110  SourceRange Range) override;
111  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
112  void If(SourceLocation Loc, SourceRange, ConditionValueKind) override;
113  void Elif(SourceLocation Loc, SourceRange, ConditionValueKind,
114  SourceLocation) override;
115  void Ifdef(SourceLocation Loc, const Token &,
116  const MacroDefinition &) override;
117  void Ifndef(SourceLocation Loc, const Token &,
118  const MacroDefinition &) override;
119  void Else(SourceLocation Loc, SourceLocation) override;
120  void Endif(SourceLocation Loc, SourceLocation) override;
121 
122  std::unique_ptr<FileRecorder> Recorder;
123  // Set of all the modules visited. Avoids processing a module more than once.
124  llvm::DenseSet<serialization::ModuleFile *> VisitedModules;
125 
126  CompilerInstance &Compiler;
127  // Additional filesystem for replay. Provides all input files from modules.
128  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFs;
129 
130  SourceManager &Sources;
131  DiagnosticsEngine Diags;
132  LangOptions LangOpts;
133  TrivialModuleLoader ModuleLoader;
134 
135  std::unique_ptr<HeaderSearch> HeaderInfo;
136  std::unique_ptr<Preprocessor> PP;
137  bool EnteredMainFile = false;
138  bool StartedLexing = false;
139  Token CurrentToken;
140 };
141 
142 } // namespace tooling
143 } // namespace clang
144 
145 #endif // LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::tooling::ExpandModularHeadersPPCallbacks::FileRecorder
Definition: ExpandModularHeadersPPCallbacks.cpp:21
clang::tooling::ExpandModularHeadersPPCallbacks::~ExpandModularHeadersPPCallbacks
~ExpandModularHeadersPPCallbacks()
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
clang::tooling::ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks
ExpandModularHeadersPPCallbacks(CompilerInstance *Compiler, IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS)
Definition: ExpandModularHeadersPPCallbacks.cpp:67
clang::tooling::ExpandModularHeadersPPCallbacks
Handles PPCallbacks and re-runs preprocessing of the whole translation unit with modules disabled.
Definition: ExpandModularHeadersPPCallbacks.h:43
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
InMemoryFileSystem
llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > InMemoryFileSystem
Definition: IndexActionTests.cpp:109
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:38
clang::tooling::ExpandModularHeadersPPCallbacks::getPreprocessor
Preprocessor * getPreprocessor() const
Returns the preprocessor that provides callbacks for the whole translation unit, including the main f...
Definition: ExpandModularHeadersPPCallbacks.cpp:107