summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-10-13 04:53:48 +0000
committerChris Lattner <sabre@nondot.org>2009-10-13 04:53:48 +0000
commit1a6babf1f41c3c8d6a0dab98e02b7caaf9024da6 (patch)
tree7767d9db3b1ddf4f4685937d21c0295e7f019f75 /clang/lib
parentf315471e24305ee3c63960edca252ac91da5ee4a (diff)
downloadbcm5719-llvm-1a6babf1f41c3c8d6a0dab98e02b7caaf9024da6.tar.gz
bcm5719-llvm-1a6babf1f41c3c8d6a0dab98e02b7caaf9024da6.zip
make the diagnostic in the 'unused result' warning more precise
about the reason, rdar://7186119. llvm-svn: 83940
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/Expr.cpp27
-rw-r--r--clang/lib/Sema/SemaStmt.cpp19
2 files changed, 33 insertions, 13 deletions
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 98348d6efc9..6da11c1cab8 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -692,21 +692,22 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
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;
}
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 2a3b9eeea81..e8cd6b081de 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -80,6 +80,25 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
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;
}
OpenPOWER on IntegriCloud