diff options
author | Anna Zaks <ganna@apple.com> | 2012-05-30 00:34:21 +0000 |
---|---|---|
committer | Anna Zaks <ganna@apple.com> | 2012-05-30 00:34:21 +0000 |
commit | 6fcb3727e31280ba816dc86d024586b8c5933c13 (patch) | |
tree | d05afeab6af5ad9054fe4f84718075e35e7e984c /lib/Sema/SemaChecking.cpp | |
parent | 6d1de1b8663fe652903bda2927b885d1ec0f2613 (diff) |
Add fixits for memory access warnings.
Also, do not display the builtin name and macro expansion when the
function is a builtin.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@157659 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaChecking.cpp')
-rw-r--r-- | lib/Sema/SemaChecking.cpp | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 769705419e..004e9948f2 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -2755,19 +2755,49 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call, // TODO: For strncpy() and friends, this could suggest sizeof(dst) // over sizeof(src) as well. unsigned ActionIdx = 0; // Default is to suggest dereferencing. + FixItHint Fixit = FixItHint(); // Default hint. + StringRef ReadableName = FnName->getName(); + + if (isa<DeclRefExpr>(SizeOfArg)) + Fixit = FixItHint::CreateInsertion(SizeOfArg->getLocStart(), "*"); + if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) - if (UnaryOp->getOpcode() == UO_AddrOf) + if (UnaryOp->getOpcode() == UO_AddrOf) { + Fixit = FixItHint::CreateRemoval( + CharSourceRange::getTokenRange(SizeOfArg->getLocStart(), + SizeOfArg->getLocStart())); ActionIdx = 1; // If its an address-of operator, just remove it. + } if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) ActionIdx = 2; // If the pointee's size is sizeof(char), // suggest an explicit length. unsigned DestSrcSelect = (BId == Builtin::BIstrndup ? 1 : ArgIdx); - DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest, + + // If the function is defined as a builtin macro, do not show macro + // expansion. + SourceLocation SL = SizeOfArg->getExprLoc(); + SourceRange DSR = Dest->getSourceRange(); + SourceRange SSR = SizeOfArg->getSourceRange(); + SourceManager &SM = PP.getSourceManager(); + + if (SM.isMacroArgExpansion(SL)) { + ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); + SL = SM.getSpellingLoc(SL); + DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), + SM.getSpellingLoc(DSR.getEnd())); + SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), + SM.getSpellingLoc(SSR.getEnd())); + } + + DiagRuntimeBehavior(SL, Dest, PDiag(diag::warn_sizeof_pointer_expr_memaccess) - << FnName << DestSrcSelect << ActionIdx - << Dest->getSourceRange() - << SizeOfArg->getSourceRange()); + << ReadableName + << DestSrcSelect + << ActionIdx + << DSR + << SSR + << Fixit); break; } } |