Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:23

0001 //===- ExpandModularHeadersPPCallbacks.h - clang-tidy -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
0010 #define LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
0011 
0012 #include "clang/Lex/PPCallbacks.h"
0013 #include "clang/Lex/Preprocessor.h"
0014 #include "llvm/ADT/DenseSet.h"
0015 
0016 namespace llvm::vfs {
0017 class OverlayFileSystem;
0018 class InMemoryFileSystem;
0019 } // namespace llvm::vfs
0020 
0021 namespace clang {
0022 class CompilerInstance;
0023 
0024 namespace serialization {
0025 class ModuleFile;
0026 } // namespace serialization
0027 
0028 namespace tooling {
0029 
0030 /// Handles PPCallbacks and re-runs preprocessing of the whole
0031 /// translation unit with modules disabled.
0032 ///
0033 /// This way it's possible to get PPCallbacks for the whole translation unit
0034 /// including the contents of the modular headers and all their transitive
0035 /// includes.
0036 ///
0037 /// This allows existing tools based on PPCallbacks to retain their functionality
0038 /// when running with C++ modules enabled. This only works in the backwards
0039 /// compatible modules mode, i.e. when code can still be parsed in non-modular
0040 /// way.
0041 class ExpandModularHeadersPPCallbacks : public PPCallbacks {
0042 public:
0043   ExpandModularHeadersPPCallbacks(
0044       CompilerInstance *CI,
0045       IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS);
0046   ~ExpandModularHeadersPPCallbacks();
0047 
0048   /// Returns the preprocessor that provides callbacks for the whole
0049   /// translation unit, including the main file, textual headers, and modular
0050   /// headers.
0051   ///
0052   /// This preprocessor is separate from the one used by the rest of the
0053   /// compiler.
0054   Preprocessor *getPreprocessor() const;
0055 
0056 private:
0057   class FileRecorder;
0058 
0059   void handleModuleFile(serialization::ModuleFile *MF);
0060   void parseToLocation(SourceLocation Loc);
0061 
0062   // Handle PPCallbacks.
0063   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
0064                    SrcMgr::CharacteristicKind FileType,
0065                    FileID PrevFID) override;
0066 
0067   void InclusionDirective(SourceLocation DirectiveLoc,
0068                           const Token &IncludeToken, StringRef IncludedFilename,
0069                           bool IsAngled, CharSourceRange FilenameRange,
0070                           OptionalFileEntryRef IncludedFile,
0071                           StringRef SearchPath, StringRef RelativePath,
0072                           const Module *SuggestedModule, bool ModuleImported,
0073                           SrcMgr::CharacteristicKind FileType) override;
0074 
0075   void EndOfMainFile() override;
0076 
0077   // Handle all other callbacks.
0078   // Just parse to the corresponding location to generate PPCallbacks for the
0079   // corresponding range
0080   void Ident(SourceLocation Loc, StringRef) override;
0081   void PragmaDirective(SourceLocation Loc, PragmaIntroducerKind) override;
0082   void PragmaComment(SourceLocation Loc, const IdentifierInfo *,
0083                      StringRef) override;
0084   void PragmaDetectMismatch(SourceLocation Loc, StringRef, StringRef) override;
0085   void PragmaDebug(SourceLocation Loc, StringRef) override;
0086   void PragmaMessage(SourceLocation Loc, StringRef, PragmaMessageKind,
0087                      StringRef) override;
0088   void PragmaDiagnosticPush(SourceLocation Loc, StringRef) override;
0089   void PragmaDiagnosticPop(SourceLocation Loc, StringRef) override;
0090   void PragmaDiagnostic(SourceLocation Loc, StringRef, diag::Severity,
0091                         StringRef) override;
0092   void HasInclude(SourceLocation Loc, StringRef, bool, OptionalFileEntryRef,
0093                   SrcMgr::CharacteristicKind) override;
0094   void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *,
0095                              SourceLocation StateLoc, unsigned) override;
0096   void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier,
0097                      ArrayRef<int>) override;
0098   void PragmaWarningPush(SourceLocation Loc, int) override;
0099   void PragmaWarningPop(SourceLocation Loc) override;
0100   void PragmaAssumeNonNullBegin(SourceLocation Loc) override;
0101   void PragmaAssumeNonNullEnd(SourceLocation Loc) override;
0102   void MacroExpands(const Token &MacroNameTok, const MacroDefinition &,
0103                     SourceRange Range, const MacroArgs *) override;
0104   void MacroDefined(const Token &MacroNameTok,
0105                     const MacroDirective *MD) override;
0106   void MacroUndefined(const Token &, const MacroDefinition &,
0107                       const MacroDirective *Undef) override;
0108   void Defined(const Token &MacroNameTok, const MacroDefinition &,
0109                SourceRange Range) override;
0110   void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
0111   void If(SourceLocation Loc, SourceRange, ConditionValueKind) override;
0112   void Elif(SourceLocation Loc, SourceRange, ConditionValueKind,
0113             SourceLocation) override;
0114   void Ifdef(SourceLocation Loc, const Token &,
0115              const MacroDefinition &) override;
0116   void Ifndef(SourceLocation Loc, const Token &,
0117               const MacroDefinition &) override;
0118   void Else(SourceLocation Loc, SourceLocation) override;
0119   void Endif(SourceLocation Loc, SourceLocation) override;
0120 
0121   std::unique_ptr<FileRecorder> Recorder;
0122   // Set of all the modules visited. Avoids processing a module more than once.
0123   llvm::DenseSet<serialization::ModuleFile *> VisitedModules;
0124 
0125   CompilerInstance &Compiler;
0126   // Additional filesystem for replay. Provides all input files from modules.
0127   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFs;
0128 
0129   SourceManager &Sources;
0130   DiagnosticsEngine Diags;
0131   LangOptions LangOpts;
0132   TrivialModuleLoader ModuleLoader;
0133 
0134   std::unique_ptr<HeaderSearch> HeaderInfo;
0135   std::unique_ptr<Preprocessor> PP;
0136   bool EnteredMainFile = false;
0137   bool StartedLexing = false;
0138   Token CurrentToken;
0139 };
0140 
0141 } // namespace tooling
0142 } // namespace clang
0143 
0144 #endif // LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_