summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Beaumont-Gay <matthewbg@google.com>2013-01-12 00:54:16 +0000
committerMatt Beaumont-Gay <matthewbg@google.com>2013-01-12 00:54:16 +0000
commitb1e71a7d0cde313a2515bd2def492001e28ecf79 (patch)
tree683d4f0e5124111e083f0d09fa60a474aaaeac8d
parent37494a176c8bfe274375c1deef124c149ba28150 (diff)
downloadbcm5719-llvm-b1e71a7d0cde313a2515bd2def492001e28ecf79.tar.gz
bcm5719-llvm-b1e71a7d0cde313a2515bd2def492001e28ecf79.zip
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. llvm-svn: 172279
-rw-r--r--clang/include/clang/Basic/SourceManager.h12
-rw-r--r--clang/lib/Basic/SourceManager.cpp11
-rw-r--r--clang/lib/Sema/SemaStmt.cpp2
-rw-r--r--clang/test/Sema/unused-expr-system-header.c6
4 files changed, 26 insertions, 5 deletions
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index a2d7a590327..6aa53472312 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -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.
///
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index f1965e7ebda..691019152c9 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -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.
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index b738793ef96..a9d3dd5ad3c 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -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)
diff --git a/clang/test/Sema/unused-expr-system-header.c b/clang/test/Sema/unused-expr-system-header.c
index dcc8918970c..68c7e9962c5 100644
--- a/clang/test/Sema/unused-expr-system-header.c
+++ b/clang/test/Sema/unused-expr-system-header.c
@@ -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));
}
OpenPOWER on IntegriCloud