summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/llvm
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2014-07-08 14:32:17 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2014-07-08 14:32:17 +0000
commit190e2cfd7459b5acc30f12702137ce33913b85b4 (patch)
treecd97d25268318ea65f973576a3fa124afe2da34c /clang-tools-extra/clang-tidy/llvm
parenteb893a1fd688362a7809525c3b8761b77090f048 (diff)
downloadbcm5719-llvm-190e2cfd7459b5acc30f12702137ce33913b85b4.tar.gz
bcm5719-llvm-190e2cfd7459b5acc30f12702137ce33913b85b4.zip
[clang-tidy] Add a little checker for Twine locals in LLVM.
Those often cause use after free bugs and should be generally avoided. Technically it is safe to have a Twine with >=2 components in a variable but I don't think it is a good pattern to follow. The almost trivial checker comes with elaborated fix-it hints that turn the Twine into a std::string if necessary and otherwise fall back to the original type if the Twine is created from a single value. llvm-svn: 212535
Diffstat (limited to 'clang-tools-extra/clang-tidy/llvm')
-rw-r--r--clang-tools-extra/clang-tidy/llvm/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp4
-rw-r--r--clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.cpp64
-rw-r--r--clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.h31
4 files changed, 100 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
index e3314d8eef3..d405874d347 100644
--- a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt
@@ -4,6 +4,7 @@ add_clang_library(clangTidyLLVMModule
IncludeOrderCheck.cpp
LLVMTidyModule.cpp
NamespaceCommentCheck.cpp
+ TwineLocalCheck.cpp
LINK_LIBS
clangAST
diff --git a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
index 31a114eeebd..0586525228b 100644
--- a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
@@ -12,6 +12,7 @@
#include "../ClangTidyModuleRegistry.h"
#include "IncludeOrderCheck.h"
#include "NamespaceCommentCheck.h"
+#include "TwineLocalCheck.h"
namespace clang {
namespace tidy {
@@ -24,6 +25,9 @@ public:
CheckFactories.addCheckFactory(
"llvm-namespace-comment",
new ClangTidyCheckFactory<NamespaceCommentCheck>());
+ CheckFactories.addCheckFactory(
+ "llvm-twine-local",
+ new ClangTidyCheckFactory<TwineLocalCheck>());
}
};
diff --git a/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.cpp b/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.cpp
new file mode 100644
index 00000000000..f30628e23b6
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.cpp
@@ -0,0 +1,64 @@
+//===--- TwineLocalCheck.cpp - clang-tidy ---------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TwineLocalCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+
+TwineLocalCheck::TwineLocalCheck() {}
+
+void TwineLocalCheck::registerMatchers(MatchFinder *Finder) {
+ auto TwineType =
+ qualType(hasDeclaration(recordDecl(hasName("::llvm::Twine"))));
+ Finder->addMatcher(varDecl(hasType(TwineType)).bind("variable"), this);
+}
+
+void TwineLocalCheck::check(const MatchFinder::MatchResult &Result) {
+ const VarDecl *VD = Result.Nodes.getNodeAs<VarDecl>("variable");
+ auto Diag = diag(VD->getLocation(),
+ "twine variables are prone to use after free bugs");
+
+ // If this VarDecl has an initializer try to fix it.
+ if (VD->hasInit()) {
+ // Peel away implicit constructors and casts so we can see the actual type
+ // of the initializer.
+ const Expr *C = VD->getInit();
+ while (isa<CXXConstructExpr>(C))
+ C = cast<CXXConstructExpr>(C)->getArg(0)->IgnoreParenImpCasts();
+
+ SourceRange TypeRange =
+ VD->getTypeSourceInfo()->getTypeLoc().getSourceRange();
+
+ // A real Twine, turn it into a std::string.
+ if (VD->getType()->getCanonicalTypeUnqualified() ==
+ C->getType()->getCanonicalTypeUnqualified()) {
+ SourceLocation EndLoc = Lexer::getLocForEndOfToken(
+ VD->getInit()->getLocEnd(), 0, *Result.SourceManager,
+ Result.Context->getLangOpts());
+ Diag << FixItHint::CreateReplacement(TypeRange, "std::string")
+ << FixItHint::CreateInsertion(VD->getInit()->getLocStart(), "(")
+ << FixItHint::CreateInsertion(EndLoc, ").str()");
+ } else {
+ // Just an implicit conversion. Insert the real type.
+ Diag << FixItHint::CreateReplacement(
+ TypeRange,
+ C->getType().getAsString(Result.Context->getPrintingPolicy()));
+ }
+ }
+}
+
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.h b/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.h
new file mode 100644
index 00000000000..f538f81430c
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/llvm/TwineLocalCheck.h
@@ -0,0 +1,31 @@
+//===--- TwineLocalCheck.h - clang-tidy -------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
+
+#include "../ClangTidy.h"
+#include "llvm/Support/Regex.h"
+
+namespace clang {
+namespace tidy {
+
+/// \brief Looks for local Twine variables which are prone to use after frees
+/// and should be generally avoided.
+class TwineLocalCheck : public ClangTidyCheck {
+public:
+ TwineLocalCheck();
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
OpenPOWER on IntegriCloud