[PATCH] D25990: Sema: do not warn about unused const vars if main file is a header

Erik Verbruggen via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 26 07:52:34 PDT 2016


erikjv created this revision.
erikjv added a reviewer: bkramer.
erikjv added a subscriber: cfe-commits.

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.


https://reviews.llvm.org/D25990

Files:
  lib/Sema/Sema.cpp
  test/Sema/no-warn-unused-const-variables.c


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 };
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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25990.75891.patch
Type: text/x-patch
Size: 1341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161026/c6b307d3/attachment.bin>


More information about the cfe-commits mailing list