Index: lib/Sema/Sema.cpp =================================================================== --- lib/Sema/Sema.cpp +++ lib/Sema/Sema.cpp @@ -865,8 +865,11 @@ 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(); Index: test/Sema/no-warn-unused-const-variables.c =================================================================== --- /dev/null +++ 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 };