From 53e767bf6b84acd7e73a8c3a5354130655021181 Mon Sep 17 00:00:00 2001 From: Matt Beaumont-Gay Date: Wed, 24 Oct 2012 01:14:28 +0000 Subject: [PATCH] Address feedback from Eli Friedman on r166522. In particular, we do want to warn on some unused cast subexpressions within macros. llvm-svn: 166534 --- clang/lib/AST/Expr.cpp | 8 ++++---- clang/test/Sema/unused-expr.c | 12 ++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 68bce4b..6f4c8e2 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -2026,10 +2026,6 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc, } case CXXFunctionalCastExprClass: case CStyleCastExprClass: { - // Ignore casts within macro expansions. - if (getExprLoc().isMacroID()) - return false; - // Ignore an explicit cast to void unless the operand is a non-trivial // volatile lvalue. const CastExpr *CE = cast(this); @@ -2047,6 +2043,10 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc, return false; } + // Ignore casts within macro expansions. + if (getExprLoc().isMacroID()) + return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); + // If this is a cast to a constructor conversion, check the operand. // Otherwise, the result of the cast is unused. if (CE->getCastKind() == CK_ConstructorConversion) diff --git a/clang/test/Sema/unused-expr.c b/clang/test/Sema/unused-expr.c index 6677e48..aa81feb 100644 --- a/clang/test/Sema/unused-expr.c +++ b/clang/test/Sema/unused-expr.c @@ -123,9 +123,13 @@ void f(int i, ...) { // PR8371 int fn5() __attribute__ ((__const)); -// OpenSSL has some macros like this. -#define M(a, b) (long)foo((a), (b)) +// OpenSSL has some macros like this; we shouldn't warn on the cast. +#define M1(a, b) (long)foo((a), (b)) +// But, we should still warn on other subexpressions of casts in macros. +#define M2 (long)0; void t11(int i, int j) { - M(i, j); // no warning + M1(i, j); // no warning + M2; // expected-warning {{expression result unused}} } -#undef M +#undef M1 +#undef M2 -- 2.7.4