1 | //===--- TwineLocalCheck.cpp - clang-tidy ---------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "TwineLocalCheck.h" |
10 | #include "clang/AST/ASTContext.h" |
11 | #include "clang/ASTMatchers/ASTMatchers.h" |
12 | #include "clang/Lex/Lexer.h" |
13 | |
14 | using namespace clang::ast_matchers; |
15 | |
16 | namespace clang::tidy::llvm_check { |
17 | |
18 | void TwineLocalCheck::registerMatchers(MatchFinder *Finder) { |
19 | auto TwineType = |
20 | qualType(hasDeclaration(InnerMatcher: cxxRecordDecl(hasName(Name: "::llvm::Twine" )))); |
21 | Finder->addMatcher( |
22 | NodeMatch: varDecl(unless(parmVarDecl()), hasType(InnerMatcher: TwineType)).bind(ID: "variable" ), |
23 | Action: this); |
24 | } |
25 | |
26 | void TwineLocalCheck::check(const MatchFinder::MatchResult &Result) { |
27 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>(ID: "variable" ); |
28 | auto Diag = diag(VD->getLocation(), |
29 | "twine variables are prone to use-after-free bugs" ); |
30 | |
31 | // If this VarDecl has an initializer try to fix it. |
32 | if (VD->hasInit()) { |
33 | // Peel away implicit constructors and casts so we can see the actual type |
34 | // of the initializer. |
35 | const Expr *C = VD->getInit()->IgnoreImplicit(); |
36 | |
37 | while (isa<CXXConstructExpr>(Val: C)) { |
38 | if (cast<CXXConstructExpr>(Val: C)->getNumArgs() == 0) |
39 | break; |
40 | C = cast<CXXConstructExpr>(Val: C)->getArg(Arg: 0)->IgnoreParenImpCasts(); |
41 | } |
42 | |
43 | SourceRange TypeRange = |
44 | VD->getTypeSourceInfo()->getTypeLoc().getSourceRange(); |
45 | |
46 | // A real Twine, turn it into a std::string. |
47 | if (VD->getType()->getCanonicalTypeUnqualified() == |
48 | C->getType()->getCanonicalTypeUnqualified()) { |
49 | SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
50 | Loc: VD->getInit()->getEndLoc(), Offset: 0, SM: *Result.SourceManager, LangOpts: getLangOpts()); |
51 | Diag << FixItHint::CreateReplacement(RemoveRange: TypeRange, Code: "std::string" ) |
52 | << FixItHint::CreateInsertion(InsertionLoc: VD->getInit()->getBeginLoc(), Code: "(" ) |
53 | << FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: ").str()" ); |
54 | } else { |
55 | // Just an implicit conversion. Insert the real type. |
56 | Diag << FixItHint::CreateReplacement( |
57 | RemoveRange: TypeRange, |
58 | Code: C->getType().getAsString(Policy: Result.Context->getPrintingPolicy())); |
59 | } |
60 | } |
61 | } |
62 | |
63 | } // namespace clang::tidy::llvm_check |
64 | |