Skip to content

Commit

Permalink
make sure to not warn about unused macros from -D
Browse files Browse the repository at this point in the history
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
  • Loading branch information
llunak committed Apr 27, 2020
1 parent c695ea2 commit 5c8c990
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion clang/lib/Lex/PPDirectives.cpp
Expand Up @@ -2802,7 +2802,9 @@ void Preprocessor::HandleDefineDirective(
// warn-because-unused-macro set. If it gets used it will be removed from set.
if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
!Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
!MacroExpansionInDirectivesOverride) {
!MacroExpansionInDirectivesOverride &&
getSourceManager().getFileID(MI->getDefinitionLoc()) !=
getPredefinesFileID()) {
MI->setIsWarnIfUnused(true);
WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
}
Expand Down
12 changes: 12 additions & 0 deletions clang/test/PCH/cli-macro.c
@@ -0,0 +1,12 @@
// Test this without pch.
// RUN: %clang_cc1 -Wunused-macros -Dunused=1 -fsyntax-only -verify %s

// Test with pch.
// RUN: %clang_cc1 -Wunused-macros -emit-pch -o %t %s
// RUN: %clang_cc1 -Wunused-macros -Dunused=1 -include-pch %t -fsyntax-only -verify %s

// expected-no-diagnostics

// -Dunused=1 is intentionally not set for the pch.
// There still should be no unused warning for a macro from the command line.

0 comments on commit 5c8c990

Please sign in to comment.