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

Matt Beaumont-Gay matthewbg at google.com
Fri Jan 11 16:54:16 PST 2013


Author: matthewbg
Date: Fri Jan 11 18:54:16 2013
New Revision: 172279

URL: http://llvm.org/viewvc/llvm-project?rev=172279&view=rev
Log:
Fix -Wunused-comparison for comparisons in arguments to function-like macros.

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.

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

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=172279&r1=172278&r2=172279&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Fri Jan 11 18:54:16 2013
@@ -329,6 +329,11 @@
         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 @@
   /// 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.
   ///

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=172279&r1=172278&r2=172279&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Fri Jan 11 18:54:16 2013
@@ -974,11 +974,18 @@
   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.

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=172279&r1=172278&r2=172279&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Jan 11 18:54:16 2013
@@ -126,7 +126,7 @@
 
   // 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)

Modified: 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=172279&r1=172278&r2=172279&view=diff
==============================================================================
--- cfe/trunk/test/Sema/unused-expr-system-header.c (original)
+++ cfe/trunk/test/Sema/unused-expr-system-header.c Fri Jan 11 18:54:16 2013
@@ -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));
 }





More information about the cfe-commits mailing list