Skip to content

Commit 5c8c990

Browse files
committed
make sure to not warn about unused macros from -D
If a PCH is used for compilation, SourceManager::isInMainFile() returns true even for the "<built-in>" predefines area. Using -D only for the TU compilation may trigger -Wunused-macros for it. It is admitedly a bit fishy to set a macro only for a TU and not for the PCH, but this works fine if the PCH does not use the macro (I couldn't find a statement on this for Clang, but GCC explicitly allows this in the docs). Differential Revision: https://reviews.llvm.org/D73846
1 parent c695ea2 commit 5c8c990

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Lex/PPDirectives.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2802,7 +2802,9 @@ void Preprocessor::HandleDefineDirective(
28022802
// warn-because-unused-macro set. If it gets used it will be removed from set.
28032803
if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
28042804
!Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
2805-
!MacroExpansionInDirectivesOverride) {
2805+
!MacroExpansionInDirectivesOverride &&
2806+
getSourceManager().getFileID(MI->getDefinitionLoc()) !=
2807+
getPredefinesFileID()) {
28062808
MI->setIsWarnIfUnused(true);
28072809
WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
28082810
}

clang/test/PCH/cli-macro.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Test this without pch.
2+
// RUN: %clang_cc1 -Wunused-macros -Dunused=1 -fsyntax-only -verify %s
3+
4+
// Test with pch.
5+
// RUN: %clang_cc1 -Wunused-macros -emit-pch -o %t %s
6+
// RUN: %clang_cc1 -Wunused-macros -Dunused=1 -include-pch %t -fsyntax-only -verify %s
7+
8+
// expected-no-diagnostics
9+
10+
// -Dunused=1 is intentionally not set for the pch.
11+
// There still should be no unused warning for a macro from the command line.
12+

0 commit comments

Comments
 (0)