Skip to content

Commit ac69f9d

Browse files
authored
[clang-tidy] Rename 'cert-err52-cpp' to 'modernize-avoid-setjmp-longjmp' (llvm#159813)
Moves the implementation of the `cert-err52-cpp` check into `modernize` module and gives it a clearer name: `modernize-avoid-setjmp-longjmp`. This is part of the cleanup described in llvm#157287. Closes llvm#157297
1 parent e52792e commit ac69f9d

File tree

11 files changed

+54
-27
lines changed

11 files changed

+54
-27
lines changed

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "../misc/NonCopyableObjects.h"
2929
#include "../misc/StaticAssertCheck.h"
3030
#include "../misc/ThrowByValueCatchByReferenceCheck.h"
31+
#include "../modernize/AvoidSetjmpLongjmpCheck.h"
3132
#include "../modernize/AvoidVariadicFunctionsCheck.h"
3233
#include "../performance/MoveConstructorInitCheck.h"
3334
#include "../readability/EnumInitialValueCheck.h"
@@ -40,14 +41,13 @@
4041
#include "MutatingCopyCheck.h"
4142
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
4243
#include "ProperlySeededRandomGeneratorCheck.h"
43-
#include "SetLongJmpCheck.h"
4444
#include "ThrownExceptionTypeCheck.h"
4545

4646
namespace {
4747

4848
// Checked functions for cert-err33-c.
49-
// The following functions are deliberately excluded because they can be called
50-
// with NULL argument and in this case the check is not applicable:
49+
// The following functions are deliberately excluded because they can be
50+
// called with NULL argument and in this case the check is not applicable:
5151
// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
5252
// FIXME: The check can be improved to handle such cases.
5353
const llvm::StringRef CertErr33CCheckedFunctions = "^::aligned_alloc$;"
@@ -257,7 +257,8 @@ class CERTModule : public ClangTidyModule {
257257
// ERR
258258
CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
259259
"cert-err09-cpp");
260-
CheckFactories.registerCheck<SetLongJmpCheck>("cert-err52-cpp");
260+
CheckFactories.registerCheck<modernize::AvoidSetjmpLongjmpCheck>(
261+
"cert-err52-cpp");
261262
CheckFactories.registerCheck<bugprone::ThrowingStaticInitializationCheck>(
262263
"cert-err58-cpp");
263264
CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");

clang-tools-extra/clang-tidy/cert/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ add_clang_library(clangTidyCERTModule STATIC
1313
MutatingCopyCheck.cpp
1414
NonTrivialTypesLibcMemoryCallsCheck.cpp
1515
ProperlySeededRandomGeneratorCheck.cpp
16-
SetLongJmpCheck.cpp
1716
ThrownExceptionTypeCheck.cpp
1817

1918
LINK_LIBS

clang-tools-extra/clang-tidy/cert/SetLongJmpCheck.cpp renamed to clang-tools-extra/clang-tidy/modernize/AvoidSetjmpLongjmpCheck.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "SetLongJmpCheck.h"
9+
#include "AvoidSetjmpLongjmpCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "clang/Frontend/CompilerInstance.h"
@@ -15,17 +15,18 @@
1515

1616
using namespace clang::ast_matchers;
1717

18-
namespace clang::tidy::cert {
18+
namespace clang::tidy::modernize {
1919

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

2424
class SetJmpMacroCallbacks : public PPCallbacks {
25-
SetLongJmpCheck &Check;
25+
AvoidSetjmpLongjmpCheck &Check;
2626

2727
public:
28-
explicit SetJmpMacroCallbacks(SetLongJmpCheck &Check) : Check(Check) {}
28+
explicit SetJmpMacroCallbacks(AvoidSetjmpLongjmpCheck &Check)
29+
: Check(Check) {}
2930

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

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

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

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

65-
} // namespace clang::tidy::cert
65+
} // namespace clang::tidy::modernize

clang-tools-extra/clang-tidy/cert/SetLongJmpCheck.h renamed to clang-tools-extra/clang-tidy/modernize/AvoidSetjmpLongjmpCheck.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::cert {
14+
namespace clang::tidy::modernize {
1515

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

33-
} // namespace clang::tidy::cert
33+
} // namespace clang::tidy::modernize
3434

35-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDSETJMPLONGJMPCHECK_H

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
66
add_clang_library(clangTidyModernizeModule STATIC
77
AvoidBindCheck.cpp
88
AvoidCArraysCheck.cpp
9+
AvoidSetjmpLongjmpCheck.cpp
910
AvoidVariadicFunctionsCheck.cpp
1011
ConcatNestedNamespacesCheck.cpp
1112
DeprecatedHeadersCheck.cpp

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModuleRegistry.h"
1212
#include "AvoidBindCheck.h"
1313
#include "AvoidCArraysCheck.h"
14+
#include "AvoidSetjmpLongjmpCheck.h"
1415
#include "AvoidVariadicFunctionsCheck.h"
1516
#include "ConcatNestedNamespacesCheck.h"
1617
#include "DeprecatedHeadersCheck.h"
@@ -64,6 +65,8 @@ class ModernizeModule : public ClangTidyModule {
6465
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
6566
CheckFactories.registerCheck<AvoidBindCheck>("modernize-avoid-bind");
6667
CheckFactories.registerCheck<AvoidCArraysCheck>("modernize-avoid-c-arrays");
68+
CheckFactories.registerCheck<AvoidSetjmpLongjmpCheck>(
69+
"modernize-avoid-setjmp-longjmp");
6770
CheckFactories.registerCheck<AvoidVariadicFunctionsCheck>(
6871
"modernize-avoid-variadic-functions");
6972
CheckFactories.registerCheck<ConcatNestedNamespacesCheck>(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ New check aliases
216216
<clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>`
217217
keeping initial check as an alias to the new one.
218218

219+
- Renamed :doc:`cert-err52-cpp <clang-tidy/checks/cert/err52-cpp>` to
220+
:doc:`modernize-avoid-setjmp-longjmp
221+
<clang-tidy/checks/modernize/avoid-setjmp-longjmp>`
222+
keeping initial check as an alias to the new one.
223+
219224
- Renamed :doc:`cert-err58-cpp <clang-tidy/checks/cert/err58-cpp>` to
220225
:doc:`bugprone-throwing-static-initialization
221226
<clang-tidy/checks/bugprone/throwing-static-initialization>`

clang-tools-extra/docs/clang-tidy/checks/cert/err52-cpp.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
cert-err52-cpp
44
==============
55

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

810
This check corresponds to the CERT C++ Coding Standard rule
911
`ERR52-CPP. Do not use setjmp() or longjmp()

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ Clang-Tidy Checks
175175
:doc:`cert-dcl58-cpp <cert/dcl58-cpp>`,
176176
:doc:`cert-env33-c <cert/env33-c>`,
177177
:doc:`cert-err33-c <cert/err33-c>`,
178-
:doc:`cert-err52-cpp <cert/err52-cpp>`,
179178
:doc:`cert-err60-cpp <cert/err60-cpp>`,
180179
:doc:`cert-flp30-c <cert/flp30-c>`,
181180
:doc:`cert-mem57-cpp <cert/mem57-cpp>`,
@@ -287,6 +286,7 @@ Clang-Tidy Checks
287286
:doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes"
288287
:doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes"
289288
:doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
289+
:doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
290290
:doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`,
291291
:doc:`modernize-concat-nested-namespaces <modernize/concat-nested-namespaces>`, "Yes"
292292
:doc:`modernize-deprecated-headers <modernize/deprecated-headers>`, "Yes"
@@ -441,6 +441,7 @@ Check aliases
441441
:doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`,
442442
:doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
443443
:doc:`cert-err34-c <cert/err34-c>`, :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`,
444+
:doc:`cert-err52-cpp <cert/err52-cpp>`, :doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
444445
:doc:`cert-err58-cpp <cert/err58-cpp>`, :doc:`bugprone-throwing-static-initialization <bugprone/throwing-static-initialization>`,
445446
:doc:`cert-err61-cpp <cert/err61-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
446447
:doc:`cert-exp42-c <cert/exp42-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.. title:: clang-tidy - modernize-avoid-setjmp-longjmp
2+
3+
modernize-avoid-setjmp-longjmp
4+
==============================
5+
6+
Flags all call expressions involving ``setjmp()`` and ``longjmp()`` in C++ code.
7+
8+
Exception handling with ``throw`` and ``catch`` should be used instead.
9+
10+
References
11+
----------
12+
13+
This check corresponds to the CERT C++ Coding Standard rule
14+
`ERR52-CPP. Do not use setjmp() or longjmp()
15+
<https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=88046492>`_.

0 commit comments

Comments
 (0)