]> granicus.if.org Git - clang/commitdiff
Suppress -Wunused-value within macros from system headers.
authorMatt Beaumont-Gay <matthewbg@google.com>
Fri, 6 Jan 2012 22:43:58 +0000 (22:43 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Fri, 6 Jan 2012 22:43:58 +0000 (22:43 +0000)
Along the way, move a helper function from SemaChecking.cpp to a more
accessible home in SourceManager.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@147692 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/SourceManager.h
lib/Sema/SemaChecking.cpp
lib/Sema/SemaStmt.cpp
test/Sema/Inputs/unused-expr-system-header.h [new file with mode: 0644]
test/Sema/unused-expr-system-header.c [new file with mode: 0644]

index b1fb78ea56ef896308410db78a85a83e8415b6e4..038b8c65d099d36ab1a5c6584e114897e3eb0eb5 100644 (file)
@@ -1087,6 +1087,11 @@ public:
     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;
 
index 248998ed0bf9e3f0be0f5631bd800ff971e8608e..73ff49a338956ca7facf6a42385a998fe90d5f16 100644 (file)
@@ -3755,11 +3755,6 @@ std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
   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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
   // 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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
   // 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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
             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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
 
     // 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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
     // 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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
     }
 
     // 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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
       (!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 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
           (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, 
index d7489d418e8f30e6b0aafead0a7b1eebee7090d3..78a58964db3a7a04e74a1a1bbb87931b0447f6da 100644 (file)
@@ -152,7 +152,8 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
 
   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,
diff --git a/test/Sema/Inputs/unused-expr-system-header.h b/test/Sema/Inputs/unused-expr-system-header.h
new file mode 100644 (file)
index 0000000..72b2d95
--- /dev/null
@@ -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 )
diff --git a/test/Sema/unused-expr-system-header.c b/test/Sema/unused-expr-system-header.c
new file mode 100644 (file)
index 0000000..dcc8918
--- /dev/null
@@ -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));
+}