diff options
author | Argyrios Kyrtzidis <kyrtzidis@apple.com> | 2022-06-30 14:04:14 -0700 |
---|---|---|
committer | Argyrios Kyrtzidis <kyrtzidis@apple.com> | 2022-07-01 14:22:31 -0700 |
commit | 0d3a2b4c6601d4ff341119aa537db184197d83de (patch) | |
tree | 810fde73c5f28ca301a3f3d12f4fb8e18e8da537 /clang/lib/Frontend | |
parent | bcd153485ebf07fe79e2b843ed5f1cb74997df1b (diff) | |
download | llvm-0d3a2b4c6601d4ff341119aa537db184197d83de.zip llvm-0d3a2b4c6601d4ff341119aa537db184197d83de.tar.gz llvm-0d3a2b4c6601d4ff341119aa537db184197d83de.tar.bz2 |
[Lex] Introduce `PPCallbacks::LexedFileChanged()` preprocessor callback
This is a preprocessor callback focused on the lexed file changing, without conflating effects of line number directives and other pragmas.
A client that only cares about what files the lexer processes, like dependency generation, can use this more straightforward
callback instead of `PPCallbacks::FileChanged()`. Clients that want the pragma directive effects as well can keep using `FileChanged()`.
A use case where `PPCallbacks::LexedFileChanged()` is particularly simpler to use than `FileChanged()` is in a situation
where a client wants to keep track of lexed file changes that include changes from/to the predefines buffer, where it becomes
unnecessary complicated trying to use `FileChanged()` while filtering out the pragma directives effects callbacks.
Also take the opportunity to provide information about the prior `FileID` the `Lexer` moved from, even when entering a new file.
Differential Revision: https://reviews.llvm.org/D128947
Diffstat (limited to 'clang/lib/Frontend')
-rw-r--r-- | clang/lib/Frontend/DependencyFile.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 3a810e5..06ddb0f 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -31,23 +31,21 @@ using namespace clang; namespace { struct DepCollectorPPCallbacks : public PPCallbacks { DependencyCollector &DepCollector; - SourceManager &SM; - DiagnosticsEngine &Diags; - DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM, - DiagnosticsEngine &Diags) - : DepCollector(L), SM(SM), Diags(Diags) {} - - void FileChanged(SourceLocation Loc, FileChangeReason Reason, - SrcMgr::CharacteristicKind FileType, - FileID PrevFID) override { - if (Reason != PPCallbacks::EnterFile) + Preprocessor &PP; + DepCollectorPPCallbacks(DependencyCollector &L, Preprocessor &PP) + : DepCollector(L), PP(PP) {} + + void LexedFileChanged(FileID FID, LexedFileChangeReason Reason, + SrcMgr::CharacteristicKind FileType, FileID PrevFID, + SourceLocation Loc) override { + if (Reason != PPCallbacks::LexedFileChangeReason::EnterFile) return; // Dependency generation really does want to go all the way to the // file entry for a source location to find out what is depended on. // We do not want #line markers to affect dependency generation! - if (Optional<StringRef> Filename = SM.getNonBuiltinFilenameForID( - SM.getFileID(SM.getExpansionLoc(Loc)))) + if (Optional<StringRef> Filename = + PP.getSourceManager().getNonBuiltinFilenameForID(FID)) DepCollector.maybeAddDependency( llvm::sys::path::remove_leading_dotslash(*Filename), /*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false, @@ -90,7 +88,9 @@ struct DepCollectorPPCallbacks : public PPCallbacks { /*IsMissing=*/false); } - void EndOfMainFile() override { DepCollector.finishedMainFile(Diags); } + void EndOfMainFile() override { + DepCollector.finishedMainFile(PP.getDiagnostics()); + } }; struct DepCollectorMMCallbacks : public ModuleMapCallbacks { @@ -175,8 +175,7 @@ bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, DependencyCollector::~DependencyCollector() { } void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { - PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>( - *this, PP.getSourceManager(), PP.getDiagnostics())); + PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*this, PP)); PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( std::make_unique<DepCollectorMMCallbacks>(*this)); } |