Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 1 | //===--- TwineLocalCheck.cpp - clang-tidy ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "TwineLocalCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
Alexander Kornienko | 0a6ce9f | 2015-03-02 12:39:18 +0000 | [diff] [blame] | 19 | namespace llvm { |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 20 | |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 21 | void TwineLocalCheck::registerMatchers(MatchFinder *Finder) { |
| 22 | auto TwineType = |
| 23 | qualType(hasDeclaration(recordDecl(hasName("::llvm::Twine")))); |
| 24 | Finder->addMatcher(varDecl(hasType(TwineType)).bind("variable"), this); |
| 25 | } |
| 26 | |
| 27 | void TwineLocalCheck::check(const MatchFinder::MatchResult &Result) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 28 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("variable"); |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 29 | auto Diag = diag(VD->getLocation(), |
Benjamin Kramer | ee58721 | 2014-07-08 15:41:20 +0000 | [diff] [blame] | 30 | "twine variables are prone to use-after-free bugs"); |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 31 | |
| 32 | // If this VarDecl has an initializer try to fix it. |
| 33 | if (VD->hasInit()) { |
| 34 | // Peel away implicit constructors and casts so we can see the actual type |
| 35 | // of the initializer. |
Tim Shen | 325c727 | 2016-06-21 20:11:20 +0000 | [diff] [blame] | 36 | const Expr *C = VD->getInit()->IgnoreImplicit(); |
| 37 | |
Yan Wang | 7696b71 | 2017-05-14 04:14:59 +0000 | [diff] [blame] | 38 | while (isa<CXXConstructExpr>(C)) { |
| 39 | if (cast<CXXConstructExpr>(C)->getNumArgs() == 0) |
| 40 | break; |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 41 | C = cast<CXXConstructExpr>(C)->getArg(0)->IgnoreParenImpCasts(); |
Yan Wang | 7696b71 | 2017-05-14 04:14:59 +0000 | [diff] [blame] | 42 | } |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 43 | |
| 44 | SourceRange TypeRange = |
| 45 | VD->getTypeSourceInfo()->getTypeLoc().getSourceRange(); |
| 46 | |
| 47 | // A real Twine, turn it into a std::string. |
| 48 | if (VD->getType()->getCanonicalTypeUnqualified() == |
| 49 | C->getType()->getCanonicalTypeUnqualified()) { |
| 50 | SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 51 | VD->getInit()->getEndLoc(), 0, *Result.SourceManager, getLangOpts()); |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 52 | Diag << FixItHint::CreateReplacement(TypeRange, "std::string") |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 53 | << FixItHint::CreateInsertion(VD->getInit()->getBeginLoc(), "(") |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 54 | << FixItHint::CreateInsertion(EndLoc, ").str()"); |
| 55 | } else { |
| 56 | // Just an implicit conversion. Insert the real type. |
| 57 | Diag << FixItHint::CreateReplacement( |
| 58 | TypeRange, |
| 59 | C->getType().getAsString(Result.Context->getPrintingPolicy())); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Alexander Kornienko | 0a6ce9f | 2015-03-02 12:39:18 +0000 | [diff] [blame] | 64 | } // namespace llvm |
Benjamin Kramer | 190e2cf | 2014-07-08 14:32:17 +0000 | [diff] [blame] | 65 | } // namespace tidy |
| 66 | } // namespace clang |