Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "../misc/NonCopyableObjects.h"
#include "../misc/StaticAssertCheck.h"
#include "../misc/ThrowByValueCatchByReferenceCheck.h"
#include "../modernize/AvoidSetjmpLongjmpCheck.h"
#include "../modernize/AvoidVariadicFunctionsCheck.h"
#include "../performance/MoveConstructorInitCheck.h"
#include "../readability/EnumInitialValueCheck.h"
Expand All @@ -40,14 +41,13 @@
#include "MutatingCopyCheck.h"
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "SetLongJmpCheck.h"
#include "ThrownExceptionTypeCheck.h"

namespace {

// Checked functions for cert-err33-c.
// The following functions are deliberately excluded because they can be called
// with NULL argument and in this case the check is not applicable:
// The following functions are deliberately excluded because they can be
// called with NULL argument and in this case the check is not applicable:
// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
// FIXME: The check can be improved to handle such cases.
const llvm::StringRef CertErr33CCheckedFunctions = "^::aligned_alloc$;"
Expand Down Expand Up @@ -257,7 +257,8 @@ class CERTModule : public ClangTidyModule {
// ERR
CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
"cert-err09-cpp");
CheckFactories.registerCheck<SetLongJmpCheck>("cert-err52-cpp");
CheckFactories.registerCheck<modernize::AvoidSetjmpLongjmpCheck>(
"cert-err52-cpp");
CheckFactories.registerCheck<bugprone::ThrowingStaticInitializationCheck>(
"cert-err58-cpp");
CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clang-tidy/cert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ add_clang_library(clangTidyCERTModule STATIC
MutatingCopyCheck.cpp
NonTrivialTypesLibcMemoryCallsCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
SetLongJmpCheck.cpp
ThrownExceptionTypeCheck.cpp

LINK_LIBS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SetLongJmpCheck.h"
#include "AvoidSetjmpLongjmpCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/CompilerInstance.h"
Expand All @@ -15,17 +15,18 @@

using namespace clang::ast_matchers;

namespace clang::tidy::cert {
namespace clang::tidy::modernize {

namespace {
const char DiagWording[] =
"do not call %0; consider using exception handling instead";

class SetJmpMacroCallbacks : public PPCallbacks {
SetLongJmpCheck &Check;
AvoidSetjmpLongjmpCheck &Check;

public:
explicit SetJmpMacroCallbacks(SetLongJmpCheck &Check) : Check(Check) {}
explicit SetJmpMacroCallbacks(AvoidSetjmpLongjmpCheck &Check)
: Check(Check) {}

void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange Range, const MacroArgs *Args) override {
Expand All @@ -39,15 +40,14 @@ class SetJmpMacroCallbacks : public PPCallbacks {
};
} // namespace

void SetLongJmpCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
void AvoidSetjmpLongjmpCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
// Per [headers]p5, setjmp must be exposed as a macro instead of a function,
// despite the allowance in C for setjmp to also be an extern function.
PP->addPPCallbacks(std::make_unique<SetJmpMacroCallbacks>(*this));
}

void SetLongJmpCheck::registerMatchers(MatchFinder *Finder) {
void AvoidSetjmpLongjmpCheck::registerMatchers(MatchFinder *Finder) {
// In case there is an implementation that happens to define setjmp as a
// function instead of a macro, this will also catch use of it. However, we
// are primarily searching for uses of longjmp.
Expand All @@ -57,9 +57,9 @@ void SetLongJmpCheck::registerMatchers(MatchFinder *Finder) {
this);
}

void SetLongJmpCheck::check(const MatchFinder::MatchResult &Result) {
void AvoidSetjmpLongjmpCheck::check(const MatchFinder::MatchResult &Result) {
const auto *E = Result.Nodes.getNodeAs<CallExpr>("expr");
diag(E->getExprLoc(), DiagWording) << cast<NamedDecl>(E->getCalleeDecl());
}

} // namespace clang::tidy::cert
} // namespace clang::tidy::modernize
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H

#include "../ClangTidyCheck.h"

namespace clang::tidy::cert {
namespace clang::tidy::modernize {

/// Guards against use of setjmp/longjmp in C++ code
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/cert/err52-cpp.html
class SetLongJmpCheck : public ClangTidyCheck {
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-setjmp-longjmp.html
class AvoidSetjmpLongjmpCheck : public ClangTidyCheck {
public:
SetLongJmpCheck(StringRef Name, ClangTidyContext *Context)
AvoidSetjmpLongjmpCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
Expand All @@ -30,6 +30,6 @@ class SetLongJmpCheck : public ClangTidyCheck {
Preprocessor *ModuleExpanderPP) override;
};

} // namespace clang::tidy::cert
} // namespace clang::tidy::modernize

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangTidyModernizeModule STATIC
AvoidBindCheck.cpp
AvoidCArraysCheck.cpp
AvoidSetjmpLongjmpCheck.cpp
AvoidVariadicFunctionsCheck.cpp
ConcatNestedNamespacesCheck.cpp
DeprecatedHeadersCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "../ClangTidyModuleRegistry.h"
#include "AvoidBindCheck.h"
#include "AvoidCArraysCheck.h"
#include "AvoidSetjmpLongjmpCheck.h"
#include "AvoidVariadicFunctionsCheck.h"
#include "ConcatNestedNamespacesCheck.h"
#include "DeprecatedHeadersCheck.h"
Expand Down Expand Up @@ -64,6 +65,8 @@ class ModernizeModule : public ClangTidyModule {
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<AvoidBindCheck>("modernize-avoid-bind");
CheckFactories.registerCheck<AvoidCArraysCheck>("modernize-avoid-c-arrays");
CheckFactories.registerCheck<AvoidSetjmpLongjmpCheck>(
"modernize-avoid-setjmp-longjmp");
CheckFactories.registerCheck<AvoidVariadicFunctionsCheck>(
"modernize-avoid-variadic-functions");
CheckFactories.registerCheck<ConcatNestedNamespacesCheck>(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ New check aliases
<clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-err52-cpp <clang-tidy/checks/cert/err52-cpp>` to
:doc:`modernize-avoid-setjmp-longjmp
<clang-tidy/checks/modernize/avoid-setjmp-longjmp>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-err58-cpp <clang-tidy/checks/cert/err58-cpp>` to
:doc:`bugprone-throwing-static-initialization
<clang-tidy/checks/bugprone/throwing-static-initialization>`
Expand Down
4 changes: 3 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/cert/err52-cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
cert-err52-cpp
==============

This check flags all call expressions involving ``setjmp()`` and ``longjmp()``.
The `cert-err52-cpp` check is an alias, please see
`modernize-avoid-setjmp-longjmp <../modernize/avoid-setjmp-longjmp.html>`_
for more information.

This check corresponds to the CERT C++ Coding Standard rule
`ERR52-CPP. Do not use setjmp() or longjmp()
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ Clang-Tidy Checks
:doc:`cert-dcl58-cpp <cert/dcl58-cpp>`,
:doc:`cert-env33-c <cert/env33-c>`,
:doc:`cert-err33-c <cert/err33-c>`,
:doc:`cert-err52-cpp <cert/err52-cpp>`,
:doc:`cert-err60-cpp <cert/err60-cpp>`,
:doc:`cert-flp30-c <cert/flp30-c>`,
:doc:`cert-mem57-cpp <cert/mem57-cpp>`,
Expand Down Expand Up @@ -287,6 +286,7 @@ Clang-Tidy Checks
:doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes"
:doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes"
:doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
:doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
:doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`,
:doc:`modernize-concat-nested-namespaces <modernize/concat-nested-namespaces>`, "Yes"
:doc:`modernize-deprecated-headers <modernize/deprecated-headers>`, "Yes"
Expand Down Expand Up @@ -441,6 +441,7 @@ Check aliases
:doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`,
:doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-err34-c <cert/err34-c>`, :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`,
:doc:`cert-err52-cpp <cert/err52-cpp>`, :doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
:doc:`cert-err58-cpp <cert/err58-cpp>`, :doc:`bugprone-throwing-static-initialization <bugprone/throwing-static-initialization>`,
:doc:`cert-err61-cpp <cert/err61-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-exp42-c <cert/exp42-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.. title:: clang-tidy - modernize-avoid-setjmp-longjmp

modernize-avoid-setjmp-longjmp
==============================

Flags all call expressions involving ``setjmp()`` and ``longjmp()`` in C++ code.

Exception handling with ``throw`` and ``catch`` should be used instead.

References
----------

This check corresponds to the CERT C++ Coding Standard rule
`ERR52-CPP. Do not use setjmp() or longjmp()
<https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=88046492>`_.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s cert-err52-cpp %t
// RUN: %check_clang_tidy %s modernize-avoid-setjmp-longjmp %t

typedef void *jmp_buf;
extern int __setjmpimpl(jmp_buf);
Expand All @@ -13,7 +13,7 @@ using ::longjmp;
static jmp_buf env;
void g() {
std::longjmp(env, 1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not call 'longjmp'; consider using exception handling instead [cert-err52-cpp]
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not call 'longjmp'; consider using exception handling instead [modernize-avoid-setjmp-longjmp]
::longjmp(env, 1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not call 'longjmp'; consider using exception handling instead
longjmp(env, 1);
Expand Down
Loading