[cfe-commits] r83940 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticSemaKinds.td lib/AST/Expr.cpp lib/Sema/SemaStmt.cpp test/Sema/attr-warn_unused_result.c test/Sema/unused-expr.c
Chris Lattner
sabre at nondot.org
Mon Oct 12 21:53:48 PDT 2009
Author: lattner
Date: Mon Oct 12 23:53:48 2009
New Revision: 83940
URL: http://llvm.org/viewvc/llvm-project?rev=83940&view=rev
Log:
make the diagnostic in the 'unused result' warning more precise
about the reason, rdar://7186119.
Modified:
cfe/trunk/include/clang/AST/Expr.h
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/Sema/SemaStmt.cpp
cfe/trunk/test/Sema/attr-warn_unused_result.c
cfe/trunk/test/Sema/unused-expr.c
Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Oct 12 23:53:48 2009
@@ -991,6 +991,9 @@
/// \brief If the callee is a FunctionDecl, return it. Otherwise return 0.
FunctionDecl *getDirectCallee();
+ const FunctionDecl *getDirectCallee() const {
+ return const_cast<CallExpr*>(this)->getDirectCallee();
+ }
/// getNumArgs - Return the number of actual arguments to this call.
///
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Oct 12 23:53:48 2009
@@ -1859,6 +1859,9 @@
def warn_unused_property_expr : Warning<
"property access result unused - getters should not have side effects">,
InGroup<UnusedValue>;
+def warn_unused_call : Warning<
+ "ignoring return value of function declared with %0 attribute">,
+ InGroup<UnusedValue>;
def err_incomplete_type_used_in_type_trait_expr : Error<
"incomplete type %0 used in type trait expression">;
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Oct 12 23:53:48 2009
@@ -692,21 +692,22 @@
case CXXMemberCallExprClass: {
// If this is a direct call, get the callee.
const CallExpr *CE = cast<CallExpr>(this);
- const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
- if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
+ if (const FunctionDecl *FD = CE->getDirectCallee()) {
// If the callee has attribute pure, const, or warn_unused_result, warn
// about it. void foo() { strlen("bar"); } should warn.
- if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
- if (FD->getAttr<WarnUnusedResultAttr>() ||
- FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
- Loc = CE->getCallee()->getLocStart();
- R1 = CE->getCallee()->getSourceRange();
-
- if (unsigned NumArgs = CE->getNumArgs())
- R2 = SourceRange(CE->getArg(0)->getLocStart(),
- CE->getArg(NumArgs-1)->getLocEnd());
- return true;
- }
+ //
+ // Note: If new cases are added here, DiagnoseUnusedExprResult should be
+ // updated to match for QoI.
+ if (FD->getAttr<WarnUnusedResultAttr>() ||
+ FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
+ Loc = CE->getCallee()->getLocStart();
+ R1 = CE->getCallee()->getSourceRange();
+
+ if (unsigned NumArgs = CE->getNumArgs())
+ R2 = SourceRange(CE->getArg(0)->getLocStart(),
+ CE->getArg(NumArgs-1)->getLocEnd());
+ return true;
+ }
}
return false;
}
Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Oct 12 23:53:48 2009
@@ -80,6 +80,25 @@
E = E->IgnoreParens();
if (isa<ObjCImplicitSetterGetterRefExpr>(E))
DiagID = diag::warn_unused_property_expr;
+
+ if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
+ // If the callee has attribute pure, const, or warn_unused_result, warn with
+ // a more specific message to make it clear what is happening.
+ if (const FunctionDecl *FD = CE->getDirectCallee()) {
+ if (FD->getAttr<WarnUnusedResultAttr>()) {
+ Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
+ return;
+ }
+ if (FD->getAttr<PureAttr>()) {
+ Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
+ return;
+ }
+ if (FD->getAttr<ConstAttr>()) {
+ Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
+ return;
+ }
+ }
+ }
Diag(Loc, DiagID) << R1 << R2;
}
Modified: cfe/trunk/test/Sema/attr-warn_unused_result.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-warn_unused_result.c?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-warn_unused_result.c (original)
+++ cfe/trunk/test/Sema/attr-warn_unused_result.c Mon Oct 12 23:53:48 2009
@@ -9,9 +9,9 @@
if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings
return -1;
- fn1(); // expected-warning {{expression result unused}}
- fn2(92, 21); // expected-warning {{expression result unused}}
- fn3(42); // expected-warning {{expression result unused}}
+ fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
+ fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}}
+ fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}}
return 0;
}
Modified: cfe/trunk/test/Sema/unused-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unused-expr.c?rev=83940&r1=83939&r2=83940&view=diff
==============================================================================
--- cfe/trunk/test/Sema/unused-expr.c (original)
+++ cfe/trunk/test/Sema/unused-expr.c Mon Oct 12 23:53:48 2009
@@ -25,7 +25,7 @@
__real__ VC;
// We know this can't change errno because of -fno-math-errno.
- sqrt(A); // expected-warning {{expression result unused}}
+ sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}}
}
extern void t1();
@@ -73,4 +73,8 @@
for (;;b == 1) {} // expected-warning{{expression result unused}}
}
-
+// rdar://7186119
+int t5f(void) __attribute__((warn_unused_result));
+void t5() {
+ t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}}
+}
More information about the cfe-commits
mailing list