-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Add more checks for functions which should be noexcept
Added new checks - `performance-noexcept-destructor` - `performance-noexcept-swap` Also added cppcoreguidlines aliases for the 2 new checks as well as `performance-noexcept-move-constructor` This fixes #62154 Reviewed By: PiotrZSL Differential Revision: https://reviews.llvm.org/D148697
- Loading branch information
Showing
22 changed files
with
945 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
clang-tools-extra/clang-tidy/performance/NoexceptDestructorCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//===--- NoexceptDestructorCheck.cpp - clang-tidy -------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "NoexceptDestructorCheck.h" | ||
#include "../utils/LexerUtils.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::performance { | ||
|
||
void NoexceptDestructorCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
functionDecl(unless(isDeleted()), cxxDestructorDecl()).bind("decl"), | ||
this); | ||
} | ||
|
||
void NoexceptDestructorCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("decl"); | ||
assert(FuncDecl); | ||
|
||
if (SpecAnalyzer.analyze(FuncDecl) != | ||
utils::ExceptionSpecAnalyzer::State::Throwing) | ||
return; | ||
|
||
// Don't complain about nothrow(false), but complain on nothrow(expr) | ||
// where expr evaluates to false. | ||
const auto *ProtoType = FuncDecl->getType()->castAs<FunctionProtoType>(); | ||
const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); | ||
if (NoexceptExpr) { | ||
NoexceptExpr = NoexceptExpr->IgnoreImplicit(); | ||
if (!isa<CXXBoolLiteralExpr>(NoexceptExpr)) { | ||
diag(NoexceptExpr->getExprLoc(), | ||
"noexcept specifier on the destructor evaluates to 'false'"); | ||
} | ||
return; | ||
} | ||
|
||
auto Diag = diag(FuncDecl->getLocation(), "destructors should " | ||
"be marked noexcept"); | ||
|
||
// Add FixIt hints. | ||
const SourceManager &SM = *Result.SourceManager; | ||
|
||
const SourceLocation NoexceptLoc = | ||
utils::lexer::getLocationForNoexceptSpecifier(FuncDecl, SM); | ||
if (NoexceptLoc.isValid()) | ||
Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept "); | ||
} | ||
|
||
} // namespace clang::tidy::performance |
42 changes: 42 additions & 0 deletions
42
clang-tools-extra/clang-tidy/performance/NoexceptDestructorCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- NoexceptDestructorCheck.h - clang-tidy -----------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTDESTRUCTORCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTDESTRUCTORCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
#include "../utils/ExceptionSpecAnalyzer.h" | ||
|
||
namespace clang::tidy::performance { | ||
|
||
/// The check flags destructors not marked with `noexcept` or marked | ||
/// with `noexcept(expr)` where `expr` evaluates to `false` | ||
/// (but is not a `false` literal itself). | ||
/// | ||
/// For the user-facing documentation see: | ||
/// https://clang.llvm.org/extra/clang-tidy/checks/performance/noexcept-destructor.html | ||
class NoexceptDestructorCheck : public ClangTidyCheck { | ||
public: | ||
NoexceptDestructorCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; | ||
} | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
|
||
private: | ||
utils::ExceptionSpecAnalyzer SpecAnalyzer; | ||
}; | ||
|
||
} // namespace clang::tidy::performance | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTDESTRUCTORCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//===--- NoexceptSwapCheck.cpp - clang-tidy -------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "NoexceptSwapCheck.h" | ||
#include "../utils/LexerUtils.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/Lex/Lexer.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::performance { | ||
|
||
void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
functionDecl(unless(isDeleted()), hasName("swap")).bind("decl"), this); | ||
} | ||
|
||
void NoexceptSwapCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("decl"); | ||
assert(FuncDecl); | ||
|
||
if (SpecAnalyzer.analyze(FuncDecl) != | ||
utils::ExceptionSpecAnalyzer::State::Throwing) | ||
return; | ||
|
||
// Don't complain about nothrow(false), but complain on nothrow(expr) | ||
// where expr evaluates to false. | ||
const auto *ProtoType = FuncDecl->getType()->castAs<FunctionProtoType>(); | ||
const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); | ||
if (NoexceptExpr) { | ||
NoexceptExpr = NoexceptExpr->IgnoreImplicit(); | ||
if (!isa<CXXBoolLiteralExpr>(NoexceptExpr)) { | ||
diag(NoexceptExpr->getExprLoc(), | ||
"noexcept specifier on swap function evaluates to 'false'"); | ||
} | ||
return; | ||
} | ||
|
||
auto Diag = diag(FuncDecl->getLocation(), "swap functions should " | ||
"be marked noexcept"); | ||
|
||
// Add FixIt hints. | ||
const SourceManager &SM = *Result.SourceManager; | ||
|
||
const SourceLocation NoexceptLoc = | ||
utils::lexer::getLocationForNoexceptSpecifier(FuncDecl, SM); | ||
if (NoexceptLoc.isValid()) | ||
Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept "); | ||
} | ||
|
||
} // namespace clang::tidy::performance |
42 changes: 42 additions & 0 deletions
42
clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- NoexceptSwapCheck.h - clang-tidy -----------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTSWAPCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTSWAPCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
#include "../utils/ExceptionSpecAnalyzer.h" | ||
|
||
namespace clang::tidy::performance { | ||
|
||
/// The check flags swap functions not marked with `noexcept` or marked | ||
/// with `noexcept(expr)` where `expr` evaluates to `false` | ||
/// (but is not a `false` literal itself). | ||
/// | ||
/// For the user-facing documentation see: | ||
/// https://clang.llvm.org/extra/clang-tidy/checks/performance/noexcept-swap.html | ||
class NoexceptSwapCheck : public ClangTidyCheck { | ||
public: | ||
NoexceptSwapCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; | ||
} | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
|
||
private: | ||
utils::ExceptionSpecAnalyzer SpecAnalyzer; | ||
}; | ||
|
||
} // namespace clang::tidy::performance | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOEXCEPTSWAPCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.