]> granicus.if.org Git - clang/commitdiff
Fix -Wunused-comparison for comparisons in arguments to function-like macros.
authorMatt Beaumont-Gay <matthewbg@google.com>
Sat, 12 Jan 2013 00:54:16 +0000 (00:54 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Sat, 12 Jan 2013 00:54:16 +0000 (00:54 +0000)
Previously, -Wunused-comparison ignored comparisons in both macro bodies and
macro arguments, but we would still emit a -Wunused-value warning for either.
Now we correctly emit -Wunused-comparison for expressions in macro arguments.

Also, add isMacroBodyExpansion to SourceManager, to go along with
isMacroArgExpansion.

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

include/clang/Basic/SourceManager.h
lib/Basic/SourceManager.cpp
lib/Sema/SemaStmt.cpp
test/Sema/unused-expr-system-header.c

index a2d7a5903270a19d679390aebd4415c0bdba0941..6aa5347231258e7cdca60f9588d33d89b80232f7 100644 (file)
@@ -329,6 +329,11 @@ namespace SrcMgr {
         SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
     }
 
+    bool isMacroBodyExpansion() const {
+      return getExpansionLocStart().isValid() &&
+        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isValid();
+    }
+
     bool isFunctionMacroExpansion() const {
       return getExpansionLocStart().isValid() &&
           getExpansionLocStart() != getExpansionLocEnd();
@@ -1126,6 +1131,13 @@ public:
   /// expanded.
   bool isMacroArgExpansion(SourceLocation Loc) const;
 
+  /// \brief Tests whether the given source location represents the expansion of
+  /// a macro body.
+  ///
+  /// This is equivalent to testing whether the location is part of a macro
+  /// expansion but not the expansion of an argument to a function-like macro.
+  bool isMacroBodyExpansion(SourceLocation Loc) const;
+
   /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
   /// chunk of the source location address space.
   ///
index f1965e7ebda5567920a811c46113d030fb1186b7..691019152c907e81f9d3e9f965924aad52731eef 100644 (file)
@@ -974,11 +974,18 @@ bool SourceManager::isMacroArgExpansion(SourceLocation Loc) const {
   if (!Loc.isMacroID()) return false;
 
   FileID FID = getFileID(Loc);
-  const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
-  const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
+  const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
   return Expansion.isMacroArgExpansion();
 }
 
+bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const {
+  if (!Loc.isMacroID()) return false;
+
+  FileID FID = getFileID(Loc);
+  const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
+  return Expansion.isMacroBodyExpansion();
+}
+
 
 //===----------------------------------------------------------------------===//
 // Queries about the code at a SourceLocation.
index b738793ef963a2b1de2ec4423ed7755a26503b46..a9d3dd5ad3c97ba75fea772fb1e51c799df1d972 100644 (file)
@@ -126,7 +126,7 @@ static bool DiagnoseUnusedComparison(Sema &S, const Expr *E) {
 
   // Suppress warnings when the operator, suspicious as it may be, comes from
   // a macro expansion.
-  if (Loc.isMacroID())
+  if (S.SourceMgr.isMacroBodyExpansion(Loc))
     return false;
 
   S.Diag(Loc, diag::warn_unused_comparison)
index dcc8918970c002644eba202a3ede5e7b848c13dd..68c7e9962c5f1e4058d8b22917f3e3b73ad12743 100644 (file)
@@ -3,8 +3,10 @@
 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_1(i1 == i2, f(i1, i2)); // expected-warning {{comparison result unused}} \
+                                      // expected-note {{equality comparison}}
   COMMA_MACRO_2(i1 == i2, f(i1, i2));
-  COMMA_MACRO_3(i1 == i2, f(i1, i2)); // expected-warning {{expression result unused}}
+  COMMA_MACRO_3(i1 == i2, f(i1, i2)); // expected-warning {{comparison result unused}} \
+                                      // expected-note {{equality comparison}}
   COMMA_MACRO_4(i1 == i2, f(i1, i2));
 }