Add a new module for the C++ Core Guidelines, and the first checker for those guidelines: cppcoreguidelines-pro-type-reinterpret-cast.

Patch by Matthias Gehre!

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@249399 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Aaron Ballman 2015-10-06 13:31:00 +00:00
Родитель fcf78a42ad
Коммит d2ec07d917
14 изменённых файлов: 159 добавлений и 2 удалений

Просмотреть файл

@ -28,6 +28,7 @@ add_clang_library(clangTidy
add_subdirectory(tool)
add_subdirectory(cert)
add_subdirectory(llvm)
add_subdirectory(cppcoreguidelines)
add_subdirectory(google)
add_subdirectory(misc)
add_subdirectory(modernize)

Просмотреть файл

@ -11,6 +11,6 @@ CLANG_LEVEL := ../../..
LIBRARYNAME := clangTidy
include $(CLANG_LEVEL)/../../Makefile.config
DIRS = utils cert readability llvm google misc modernize tool
DIRS = utils cert cppcoreguidelines readability llvm google misc modernize tool
include $(CLANG_LEVEL)/Makefile

Просмотреть файл

@ -147,7 +147,8 @@ void %(check_name)s::check(const MatchFinder::MatchResult &Result) {
# Modifies the module to include the new check.
def adapt_module(module_path, module, check_name, check_name_camel):
filename = os.path.join(module_path, module.capitalize() + 'TidyModule.cpp')
modulecpp = filter(lambda p: p.lower() == module.lower() + "tidymodule.cpp", os.listdir(module_path))[0]
filename = os.path.join(module_path, modulecpp)
with open(filename, 'r') as f:
lines = f.readlines()

Просмотреть файл

@ -0,0 +1,15 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyCppCoreGuidelinesModule
CppCoreGuidelinesTidyModule.cpp
ProTypeReinterpretCastCheck.cpp
LINK_LIBS
clangAST
clangASTMatchers
clangBasic
clangLex
clangTidy
clangTidyUtils
clangTooling
)

Просмотреть файл

@ -0,0 +1,38 @@
//===--- CppCoreGuidelinesModule.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 "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "ProTypeReinterpretCastCheck.h"
namespace clang {
namespace tidy {
namespace cppcoreguidelines {
class CppCoreGuidelinesModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<ProTypeReinterpretCastCheck>(
"cppcoreguidelines-pro-type-reinterpret-cast");
}
};
// Register the LLVMTidyModule using this statically initialized variable.
static ClangTidyModuleRegistry::Add<CppCoreGuidelinesModule>
X("cppcoreguidelines-module", "Adds checks for the C++ Core Guidelines.");
} // namespace cppcoreguidelines
// This anchor is used to force the linker to link in the generated object file
// and thus register the CppCoreGuidelinesModule.
volatile int CppCoreGuidelinesModuleAnchorSource = 0;
} // namespace tidy
} // namespace clang

Просмотреть файл

@ -0,0 +1,12 @@
##===- clang-tidy/cppcoreguidelines/Makefile ----------------------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
CLANG_LEVEL := ../../../..
LIBRARYNAME := clangTidyCppCoreGuidelinesModule
include $(CLANG_LEVEL)/Makefile

Просмотреть файл

@ -0,0 +1,34 @@
//===--- ProTypeReinterpretCastCheck.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 "ProTypeReinterpretCastCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
void ProTypeReinterpretCastCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
Finder->addMatcher(cxxReinterpretCastExpr().bind("cast"), this);
}
void ProTypeReinterpretCastCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedCast =
Result.Nodes.getNodeAs<CXXReinterpretCastExpr>("cast");
diag(MatchedCast->getOperatorLoc(),
"do not use reinterpret_cast");
}
} // namespace tidy
} // namespace clang

Просмотреть файл

@ -0,0 +1,33 @@
//===--- ProTypeReinterpretCast.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_CPPCOREGUIDELINES_PRO_TYPE_REINTERPRETCAST_CHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_REINTERPRETCAST_CHECK_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
/// Flags all occurrences of reinterpret_cast
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/misc-no-reinterpret-cast.html
class ProTypeReinterpretCastCheck : public ClangTidyCheck {
public:
ProTypeReinterpretCastCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
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_CPPCOREGUIDELINES_PRO_TYPE_REINTERPRETCAST_CHECK_H

Просмотреть файл

@ -11,6 +11,7 @@ target_link_libraries(clang-tidy
clangBasic
clangTidy
clangTidyCERTModule
clangTidyCppCoreGuidelinesModule
clangTidyGoogleModule
clangTidyLLVMModule
clangTidyMiscModule

Просмотреть файл

@ -357,6 +357,11 @@ extern volatile int LLVMModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
LLVMModuleAnchorSource;
// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
extern volatile int CppCoreGuidelinesModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
CppCoreGuidelinesModuleAnchorSource;
// This anchor is used to force the linker to link the GoogleModule.
extern volatile int GoogleModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =

Просмотреть файл

@ -19,6 +19,7 @@ LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc option
USEDLIBS = clangTidy.a clangTidyLLVMModule.a clangTidyGoogleModule.a \
clangTidyMiscModule.a clangTidyModernizeModule.a clangTidyReadability.a \
clangTidyUtils.a clangTidyCERTModule.a clangStaticAnalyzerFrontend.a \
clangTidyCppCoreGuidelinesModule.a \
clangStaticAnalyzerCheckers.a clangStaticAnalyzerCore.a \
clangFormat.a clangASTMatchers.a clangTooling.a clangToolingCore.a \
clangFrontend.a clangSerialization.a clangDriver.a clangParse.a \

Просмотреть файл

@ -0,0 +1,9 @@
cppcoreguidelines-pro-type-reinterpret-cast
===========================================
This check flags all uses of reinterpret_cast in C++ code.
Use of these casts can violate type safety and cause the program to access a variable that is actually of type X to be accessed as if it were of an unrelated type Z.
This rule is part of the "Type safety" profile of the C++ Core Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type1-dont-use-reinterpret_cast.

Просмотреть файл

@ -3,6 +3,7 @@ List of clang-tidy Checks
.. toctree::
cert-variadic-function-def
cppcoreguidelines-pro-type-reinterpret-cast
google-build-explicit-make-pair
google-build-namespaces
google-build-using-namespace

Просмотреть файл

@ -0,0 +1,6 @@
// RUN: %python %S/check_clang_tidy.py %s cppcoreguidelines-pro-type-reinterpret-cast %t
int i = 0;
void* j;
void f() { j = reinterpret_cast<void*>(i); }
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]