[cfe-commits] r147692 - in /cfe/trunk: include/clang/Basic/SourceManager.h lib/Sema/SemaChecking.cpp lib/Sema/SemaStmt.cpp test/Sema/Inputs/unused-expr-system-header.h test/Sema/unused-expr-system-header.c

Matt Beaumont-Gay matthewbg at google.com
Fri Jan 6 14:43:58 PST 2012


Author: matthewbg
Date: Fri Jan  6 16:43:58 2012
New Revision: 147692

URL: http://llvm.org/viewvc/llvm-project?rev=147692&view=rev
Log:
Suppress -Wunused-value within macros from system headers.

Along the way, move a helper function from SemaChecking.cpp to a more
accessible home in SourceManager.

Added:
    cfe/trunk/test/Sema/Inputs/unused-expr-system-header.h
    cfe/trunk/test/Sema/unused-expr-system-header.c
Modified:
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=147692&r1=147691&r2=147692&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Fri Jan  6 16:43:58 2012
@@ -1087,6 +1087,11 @@
     return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
   }
 
+  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
+  bool isInSystemMacro(SourceLocation loc) {
+    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
+  }
+
   /// \brief The size of the SLocEnty that \p FID represents.
   unsigned getFileIDSize(FileID FID) const;
 

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=147692&r1=147691&r2=147692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Jan  6 16:43:58 2012
@@ -3755,11 +3755,6 @@
   return ValueInRange.toString(10);
 }
 
-static bool isFromSystemMacro(Sema &S, SourceLocation loc) {
-  SourceManager &smgr = S.Context.getSourceManager();
-  return loc.isMacroID() && smgr.isInSystemHeader(smgr.getSpellingLoc(loc));
-}
-
 void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
                              SourceLocation CC, bool *ICContext = 0) {
   if (E->isTypeDependent() || E->isValueDependent()) return;
@@ -3821,7 +3816,7 @@
   // Strip vector types.
   if (isa<VectorType>(Source)) {
     if (!isa<VectorType>(Target)) {
-      if (isFromSystemMacro(S, CC))
+      if (S.SourceMgr.isInSystemMacro(CC))
         return;
       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
     }
@@ -3838,7 +3833,7 @@
   // Strip complex types.
   if (isa<ComplexType>(Source)) {
     if (!isa<ComplexType>(Target)) {
-      if (isFromSystemMacro(S, CC))
+      if (S.SourceMgr.isInSystemMacro(CC))
         return;
 
       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
@@ -3870,7 +3865,7 @@
             return;
         }
 
-        if (isFromSystemMacro(S, CC))
+        if (S.SourceMgr.isInSystemMacro(CC))
           return;
 
         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
@@ -3880,7 +3875,7 @@
 
     // If the target is integral, always warn.    
     if ((TargetBT && TargetBT->isInteger())) {
-      if (isFromSystemMacro(S, CC))
+      if (S.SourceMgr.isInSystemMacro(CC))
         return;
       
       Expr *InnerE = E->IgnoreParenImpCasts();
@@ -3917,7 +3912,7 @@
     // TODO: this should happen for bitfield stores, too.
     llvm::APSInt Value(32);
     if (E->isIntegerConstantExpr(Value, S.Context)) {
-      if (isFromSystemMacro(S, CC))
+      if (S.SourceMgr.isInSystemMacro(CC))
         return;
 
       std::string PrettySourceValue = Value.toString(10);
@@ -3932,7 +3927,7 @@
     }
 
     // People want to build with -Wshorten-64-to-32 and not -Wconversion.
-    if (isFromSystemMacro(S, CC))
+    if (S.SourceMgr.isInSystemMacro(CC))
       return;
     
     if (SourceRange.Width == 64 && TargetRange.Width == 32)
@@ -3944,7 +3939,7 @@
       (!TargetRange.NonNegative && SourceRange.NonNegative &&
        SourceRange.Width == TargetRange.Width)) {
         
-    if (isFromSystemMacro(S, CC))
+    if (S.SourceMgr.isInSystemMacro(CC))
       return;
 
     unsigned DiagID = diag::warn_impcast_integer_sign;
@@ -3982,7 +3977,7 @@
           (TargetEnum->getDecl()->getIdentifier() ||
            TargetEnum->getDecl()->getTypedefNameForAnonDecl()) &&
           SourceEnum != TargetEnum) {
-        if (isFromSystemMacro(S, CC))
+        if (S.SourceMgr.isInSystemMacro(CC))
           return;
 
         return DiagnoseImpCast(S, E, SourceType, T, CC, 

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=147692&r1=147691&r2=147692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Jan  6 16:43:58 2012
@@ -152,7 +152,8 @@
 
   SourceLocation Loc;
   SourceRange R1, R2;
-  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
+  if (SourceMgr.isInSystemMacro(E->getExprLoc()) ||
+      !E->isUnusedResultAWarning(Loc, R1, R2, Context))
     return;
 
   // Okay, we have an unused result.  Depending on what the base expression is,

Added: cfe/trunk/test/Sema/Inputs/unused-expr-system-header.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/Inputs/unused-expr-system-header.h?rev=147692&view=auto
==============================================================================
--- cfe/trunk/test/Sema/Inputs/unused-expr-system-header.h (added)
+++ cfe/trunk/test/Sema/Inputs/unused-expr-system-header.h Fri Jan  6 16:43:58 2012
@@ -0,0 +1,23 @@
+// "System header" for testing that -Wunused-value is properly suppressed in
+// certain cases.
+
+#define POSSIBLY_BAD_MACRO(x) \
+  { int i = x; \
+    i; }
+
+#define STATEMENT_EXPR_MACRO(x) \
+  (__extension__ \
+   ({int i = x; \
+     i;}))
+
+#define COMMA_MACRO_1(x, y) \
+  {x, y;}
+
+#define COMMA_MACRO_2(x, y) \
+  if (x) { 1 == 2, y; }
+
+#define COMMA_MACRO_3(x, y) \
+  (x, y)
+
+#define COMMA_MACRO_4(x, y) \
+  ( 1 == 2, y )

Added: cfe/trunk/test/Sema/unused-expr-system-header.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unused-expr-system-header.c?rev=147692&view=auto
==============================================================================
--- cfe/trunk/test/Sema/unused-expr-system-header.c (added)
+++ cfe/trunk/test/Sema/unused-expr-system-header.c Fri Jan  6 16:43:58 2012
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -isystem %S/Inputs -fsyntax-only -verify %s
+#include <unused-expr-system-header.h>
+void f(int i1, int i2) {
+  POSSIBLY_BAD_MACRO(5);
+  STATEMENT_EXPR_MACRO(5);
+  COMMA_MACRO_1(i1 == i2, f(i1, i2)); // expected-warning {{expression result unused}}
+  COMMA_MACRO_2(i1 == i2, f(i1, i2));
+  COMMA_MACRO_3(i1 == i2, f(i1, i2)); // expected-warning {{expression result unused}}
+  COMMA_MACRO_4(i1 == i2, f(i1, i2));
+}





More information about the cfe-commits mailing list