Ignoring some unused const variables

Brooks Davis brooks at freebsd.org
Fri Oct 24 14:43:36 PDT 2014


Older code often includes idioms like:

static const char rcsid[] = "$Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp $";

which are currently warned about with -Wunused-variable unless
-Wno-unused-const-variable is given.  Unfortunatly the latter masks
variables that should be removed in some cases.  The following patch causes
the three rcsid like variables I tripped on in FreeBSD's libc to always be
ingored.  I'm not sure if this fits the current warnings model, but I found
it useful.

-- Brooks

diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index ea7431d..b5d3751 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -831,8 +831,12 @@ 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();
+	  if (DiagD->getName() != StringRef("elsieid") &&
+	    DiagD->getName() != StringRef("rcsid") &&
+	    DiagD->getName() != StringRef("yysccsid")) {
+            Diag(DiagD->getLocation(), diag::warn_unused_const_variable)
+                << DiagD->getDeclName();
+	  }
         } else {
           Diag(DiagD->getLocation(), diag::warn_unused_variable)
               << DiagD->getDeclName();
diff --git a/test/SemaCXX/ignore-unused-const-src-id.cpp b/test/SemaCXX/ignore-unused-const-src-id.cpp
new file mode 100644
index 0000000..d5ff1bb
--- /dev/null
+++ b/test/SemaCXX/ignore-unused-const-src-id.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -verify %s
+
+namespace {
+  const int i = 0; // expected-warning {{unused variable 'i'}}
+  const int elsieid = 0;
+  const int rcsid = 0;
+  const int yysccsid = 0;
+}



More information about the cfe-commits mailing list