Skip to content

Commit 7b2e0d9

Browse files
committed
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
1 parent 104ad92 commit 7b2e0d9

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

clang/include/clang/Lex/PPCallbacks.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ class PPCallbacks {
370370
/// read.
371371
virtual void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
372372
OptionalFileEntryRef File,
373-
SrcMgr::CharacteristicKind FileType);
373+
SrcMgr::CharacteristicKind FileType,
374+
bool AddToDepCollector = true);
374375

375376
/// Hook called when a source range is skipped.
376377
/// \param Range The SourceRange that was skipped. The range begins at the
@@ -621,7 +622,8 @@ class PPChainedCallbacks : public PPCallbacks {
621622

622623
void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
623624
OptionalFileEntryRef File,
624-
SrcMgr::CharacteristicKind FileType) override;
625+
SrcMgr::CharacteristicKind FileType,
626+
bool AddToDepCollector = true) override;
625627

626628
void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
627629
SourceLocation StateLoc, unsigned State) override {

clang/lib/Frontend/DependencyFile.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
104104

105105
void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
106106
OptionalFileEntryRef File,
107-
SrcMgr::CharacteristicKind FileType) override {
107+
SrcMgr::CharacteristicKind FileType,
108+
bool AddToDepCollector = true) override {
108109
if (!File)
109110
return;
110111
StringRef Filename =
111112
llvm::sys::path::remove_leading_dotslash(File->getName());
112-
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
113-
/*IsSystem=*/isSystem(FileType),
114-
/*IsModuleFile=*/false,
115-
/*IsMissing=*/false);
113+
if (AddToDepCollector)
114+
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
115+
/*IsSystem=*/isSystem(FileType),
116+
/*IsModuleFile=*/false,
117+
/*IsMissing=*/false);
116118
}
117119

118120
void EndOfMainFile() override {

clang/lib/Lex/PPCallbacks.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ PPCallbacks::~PPCallbacks() = default;
1515

1616
void PPCallbacks::HasInclude(SourceLocation Loc, StringRef FileName,
1717
bool IsAngled, OptionalFileEntryRef File,
18-
SrcMgr::CharacteristicKind FileType) {}
18+
SrcMgr::CharacteristicKind FileType,
19+
bool AddToDepCollector) {}
1920

2021
// Out of line key method.
2122
PPChainedCallbacks::~PPChainedCallbacks() = default;
2223

2324
void PPChainedCallbacks::HasInclude(SourceLocation Loc, StringRef FileName,
2425
bool IsAngled, OptionalFileEntryRef File,
25-
SrcMgr::CharacteristicKind FileType) {
26-
First->HasInclude(Loc, FileName, IsAngled, File, FileType);
27-
Second->HasInclude(Loc, FileName, IsAngled, File, FileType);
26+
SrcMgr::CharacteristicKind FileType,
27+
bool AddToDepCollector) {
28+
First->HasInclude(Loc, FileName, IsAngled, File, FileType, AddToDepCollector);
29+
Second->HasInclude(Loc, FileName, IsAngled, File, FileType,
30+
AddToDepCollector);
2831
}

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,8 @@ static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
12561256
SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
12571257
if (File)
12581258
FileType = PP.getHeaderSearchInfo().getFileDirFlavor(*File);
1259-
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1259+
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType,
1260+
false);
12601261
}
12611262

12621263
// Get the result value. A result of true means the file exists.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Test -MF and -E flags with has_include
2+
3+
#ifdef TEST_HAS_INCLUDE_NEXT
4+
#if __has_include_next(<limits.h>)
5+
// DO NOTHING
6+
#endif
7+
#endif
8+
9+
#ifdef TEST_HAS_INCLUDE
10+
#if __has_include(<limits.h>)
11+
// DO NOTHING
12+
#endif
13+
#endif
14+
15+
// RUN: %clang -DTEST_HAS_INCLUDE -E -MD -MF - %s \
16+
// RUN: | FileCheck -check-prefix=TEST-HAS %s
17+
// TEST-HAS: dependencies-on-has-include.o:
18+
// TEST-HAS-NOT: limits.h
19+
20+
// RUN: %clang -Wno-include-next-outside-header -DTEST_HAS_INCLUDE_NEXT -E -MD -MF - %s \
21+
// RUN: | FileCheck -check-prefix=TEST-HAS-N %s
22+
// TEST-HAS-N: dependencies-on-has-include.o:
23+
// TEST-HAS-N-NOT: limits.h

0 commit comments

Comments
 (0)