https://github.com/wzssyqa created https://github.com/llvm/llvm-project/pull/120673
When we preprocess the bellow code with clang -E -MD -MF #if __has_include(<limits.h>) // DO NOTHING #endif #if 0 && __has_include(<limits.h>) #include <limits.h> #endif It will list limits.h in the dependencies. The same case with #__has_include_next >From 7b2e0d9dddfe25f539f6f2c811b2f23d5a244ce8 Mon Sep 17 00:00:00 2001 From: YunQiang Su <yunqi...@isrc.iscas.ac.cn> Date: Fri, 20 Dec 2024 02:55:49 +0000 Subject: [PATCH] Clang/Preprocessor: Not add headers of __has_include into DepColloctor When we preprocess the bellow code with clang -E -MD -MF #if __has_include(<limits.h>) // DO NOTHING #endif #if 0 && __has_include(<limits.h>) #include <limits.h> #endif It will list limits.h in the dependencies. The same case with #__has_include_next --- clang/include/clang/Lex/PPCallbacks.h | 6 +++-- clang/lib/Frontend/DependencyFile.cpp | 12 ++++++---- clang/lib/Lex/PPCallbacks.cpp | 11 +++++---- clang/lib/Lex/PPMacroExpansion.cpp | 3 ++- .../dependencies-on-has-include.c | 23 +++++++++++++++++++ 5 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 clang/test/Preprocessor/dependencies-on-has-include.c diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h index 46cc564086f1c5..8e03c2b9a76dbb 100644 --- a/clang/include/clang/Lex/PPCallbacks.h +++ b/clang/include/clang/Lex/PPCallbacks.h @@ -370,7 +370,8 @@ class PPCallbacks { /// read. virtual void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, - SrcMgr::CharacteristicKind FileType); + SrcMgr::CharacteristicKind FileType, + bool AddToDepCollector = true); /// Hook called when a source range is skipped. /// \param Range The SourceRange that was skipped. The range begins at the @@ -621,7 +622,8 @@ class PPChainedCallbacks : public PPCallbacks { void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, - SrcMgr::CharacteristicKind FileType) override; + SrcMgr::CharacteristicKind FileType, + bool AddToDepCollector = true) override; void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name, SourceLocation StateLoc, unsigned State) override { diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 528eae2c5283ea..931e29cd352211 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -104,15 +104,17 @@ struct DepCollectorPPCallbacks : public PPCallbacks { void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, OptionalFileEntryRef File, - SrcMgr::CharacteristicKind FileType) override { + SrcMgr::CharacteristicKind FileType, + bool AddToDepCollector = true) override { if (!File) return; StringRef Filename = llvm::sys::path::remove_leading_dotslash(File->getName()); - DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, - /*IsSystem=*/isSystem(FileType), - /*IsModuleFile=*/false, - /*IsMissing=*/false); + if (AddToDepCollector) + DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, + /*IsSystem=*/isSystem(FileType), + /*IsModuleFile=*/false, + /*IsMissing=*/false); } void EndOfMainFile() override { diff --git a/clang/lib/Lex/PPCallbacks.cpp b/clang/lib/Lex/PPCallbacks.cpp index cf473aa4df6564..79bb64892f6de6 100644 --- a/clang/lib/Lex/PPCallbacks.cpp +++ b/clang/lib/Lex/PPCallbacks.cpp @@ -15,14 +15,17 @@ PPCallbacks::~PPCallbacks() = default; void PPCallbacks::HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, - SrcMgr::CharacteristicKind FileType) {} + SrcMgr::CharacteristicKind FileType, + bool AddToDepCollector) {} // Out of line key method. PPChainedCallbacks::~PPChainedCallbacks() = default; void PPChainedCallbacks::HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, - SrcMgr::CharacteristicKind FileType) { - First->HasInclude(Loc, FileName, IsAngled, File, FileType); - Second->HasInclude(Loc, FileName, IsAngled, File, FileType); + SrcMgr::CharacteristicKind FileType, + bool AddToDepCollector) { + First->HasInclude(Loc, FileName, IsAngled, File, FileType, AddToDepCollector); + Second->HasInclude(Loc, FileName, IsAngled, File, FileType, + AddToDepCollector); } diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215..3429f08f45a2a5 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1256,7 +1256,8 @@ static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II, SrcMgr::CharacteristicKind FileType = SrcMgr::C_User; if (File) FileType = PP.getHeaderSearchInfo().getFileDirFlavor(*File); - Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType); + Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType, + false); } // Get the result value. A result of true means the file exists. diff --git a/clang/test/Preprocessor/dependencies-on-has-include.c b/clang/test/Preprocessor/dependencies-on-has-include.c new file mode 100644 index 00000000000000..b6efbb22e18477 --- /dev/null +++ b/clang/test/Preprocessor/dependencies-on-has-include.c @@ -0,0 +1,23 @@ +// Test -MF and -E flags with has_include + +#ifdef TEST_HAS_INCLUDE_NEXT +#if __has_include_next(<limits.h>) +// DO NOTHING +#endif +#endif + +#ifdef TEST_HAS_INCLUDE +#if __has_include(<limits.h>) +// DO NOTHING +#endif +#endif + +// RUN: %clang -DTEST_HAS_INCLUDE -E -MD -MF - %s \ +// RUN: | FileCheck -check-prefix=TEST-HAS %s +// TEST-HAS: dependencies-on-has-include.o: +// TEST-HAS-NOT: limits.h + +// RUN: %clang -Wno-include-next-outside-header -DTEST_HAS_INCLUDE_NEXT -E -MD -MF - %s \ +// RUN: | FileCheck -check-prefix=TEST-HAS-N %s +// TEST-HAS-N: dependencies-on-has-include.o: +// TEST-HAS-N-NOT: limits.h _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits