From 89aa7eb2ab30118dfd9c8bf5b2398cd4bc70d53e Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Fri, 28 Oct 2016 08:28:42 +0000 Subject: [PATCH] Sema: do not warn about unused const vars if main file is a header If we pass a header to libclang, e.g. because it's open in an editor in an IDE, warnings about unused const vars are not useful: other files that include the header might use those constants. So when -x *-header is passed as command-line option, suppress this warning. llvm-svn: 285386 --- clang/lib/Sema/Sema.cpp | 7 +++++-- clang/test/Sema/no-warn-unused-const-variables.c | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 clang/test/Sema/no-warn-unused-const-variables.c diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 2a3be84263c7..c2622ecab4bd 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -865,8 +865,11 @@ void Sema::ActOnEndOfTranslationUnit() { Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) << /*variable*/1 << DiagD->getDeclName(); } else if (DiagD->getType().isConstQualified()) { - Diag(DiagD->getLocation(), diag::warn_unused_const_variable) - << DiagD->getDeclName(); + const SourceManager &SM = SourceMgr; + if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) || + !PP.getLangOpts().IsHeaderFile) + Diag(DiagD->getLocation(), diag::warn_unused_const_variable) + << DiagD->getDeclName(); } else { Diag(DiagD->getLocation(), diag::warn_unused_variable) << DiagD->getDeclName(); diff --git a/clang/test/Sema/no-warn-unused-const-variables.c b/clang/test/Sema/no-warn-unused-const-variables.c new file mode 100644 index 000000000000..73cfd8ea71b5 --- /dev/null +++ b/clang/test/Sema/no-warn-unused-const-variables.c @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -fsyntax-only -Wunused-const-variable -x c-header -ffreestanding -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-const-variable -x c++-header -ffreestanding -verify %s +// expected-no-diagnostics +static const int unused[] = { 2, 3, 5, 7, 11, 13 };