forked from OSchip/llvm-project
Add checker for the C++ Core Guidelines: cppcoreguidelines-pro-type-const-cast.
Patch by Matthias Gehre! llvm-svn: 249540
This commit is contained in:
parent
ca2bc7865d
commit
0bf129823a
|
@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
|
|||
|
||||
add_clang_library(clangTidyCppCoreGuidelinesModule
|
||||
CppCoreGuidelinesTidyModule.cpp
|
||||
ProTypeConstCastCheck.cpp
|
||||
ProTypeReinterpretCastCheck.cpp
|
||||
|
||||
LINK_LIBS
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "../ClangTidy.h"
|
||||
#include "../ClangTidyModule.h"
|
||||
#include "../ClangTidyModuleRegistry.h"
|
||||
#include "ProTypeConstCastCheck.h"
|
||||
#include "ProTypeReinterpretCastCheck.h"
|
||||
|
||||
namespace clang {
|
||||
|
@ -19,6 +20,8 @@ namespace cppcoreguidelines {
|
|||
class CppCoreGuidelinesModule : public ClangTidyModule {
|
||||
public:
|
||||
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
|
||||
CheckFactories.registerCheck<ProTypeConstCastCheck>(
|
||||
"cppcoreguidelines-pro-type-const-cast");
|
||||
CheckFactories.registerCheck<ProTypeReinterpretCastCheck>(
|
||||
"cppcoreguidelines-pro-type-reinterpret-cast");
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
//===--- ProTypeConstCastCheck.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 "ProTypeConstCastCheck.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
|
||||
void ProTypeConstCastCheck::registerMatchers(MatchFinder *Finder) {
|
||||
if(!getLangOpts().CPlusPlus)
|
||||
return;
|
||||
|
||||
Finder->addMatcher(cxxConstCastExpr().bind("cast"), this);
|
||||
}
|
||||
|
||||
void ProTypeConstCastCheck::check(const MatchFinder::MatchResult &Result) {
|
||||
const auto *MatchedCast = Result.Nodes.getNodeAs<CXXConstCastExpr>("cast");
|
||||
diag(MatchedCast->getOperatorLoc(), "do not use const_cast");
|
||||
}
|
||||
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
//===--- ProTypeConstCastCheck.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_CONST_CAST_H
|
||||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_CONST_CAST_H
|
||||
|
||||
#include "../ClangTidy.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
|
||||
/// This check flags all instances of const_cast
|
||||
///
|
||||
/// For the user-facing documentation see:
|
||||
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-const-cast.html
|
||||
class ProTypeConstCastCheck : public ClangTidyCheck {
|
||||
public:
|
||||
ProTypeConstCastCheck(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_CONST_CAST_H
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
cppcoreguidelines-pro-type-const-cast
|
||||
=====================================
|
||||
|
||||
This check flags all uses of const_cast in C++ code.
|
||||
|
||||
Modifying a variable that was declared const is undefined behavior, even with const_cast.
|
||||
|
||||
This rule is part of the "Type safety" profile of the C++ Core Guidelines, see
|
||||
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type3-dont-use-const_cast-to-cast-away-const-ie-at-all.
|
|
@ -3,6 +3,7 @@ List of clang-tidy Checks
|
|||
|
||||
.. toctree::
|
||||
cert-variadic-function-def
|
||||
cppcoreguidelines-pro-type-const-cast
|
||||
cppcoreguidelines-pro-type-reinterpret-cast
|
||||
google-build-explicit-make-pair
|
||||
google-build-namespaces
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
// RUN: %python %S/check_clang_tidy.py %s cppcoreguidelines-pro-type-const-cast %t
|
||||
|
||||
const int* i;
|
||||
int* j;
|
||||
void f() { j = const_cast<int*>(i); }
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]
|
Loading…
Reference in New Issue