clang-tools 19.0.0git
NoexceptFunctionBaseCheck.cpp
Go to the documentation of this file.
1//===--- NoexceptFunctionCheck.cpp - clang-tidy ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
18
19void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) {
20 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(BindFuncDeclName);
21 assert(FuncDecl);
22
23 if (SpecAnalyzer.analyze(FuncDecl) !=
25 return;
26
27 // Don't complain about nothrow(false), but complain on nothrow(expr)
28 // where expr evaluates to false.
29 const auto *ProtoType = FuncDecl->getType()->castAs<FunctionProtoType>();
30 const Expr *NoexceptExpr = ProtoType->getNoexceptExpr();
31 if (NoexceptExpr) {
32 NoexceptExpr = NoexceptExpr->IgnoreImplicit();
33 if (!isa<CXXBoolLiteralExpr>(NoexceptExpr))
34 reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr);
35 return;
36 }
37
38 auto Diag = reportMissingNoexcept(FuncDecl);
39
40 // Add FixIt hints.
41 const SourceManager &SM = *Result.SourceManager;
42
43 const SourceLocation NoexceptLoc =
45 if (NoexceptLoc.isValid())
46 Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept ");
47}
48
49} // namespace clang::tidy::performance
void check(const ast_matchers::MatchFinder::MatchResult &Result) final override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
virtual void reportNoexceptEvaluatedToFalse(const FunctionDecl *FuncDecl, const Expr *NoexceptExpr)=0
virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl)=0
State analyze(const FunctionDecl *FuncDecl)
@ Throwing
This function has been declared as possibly throwing.
SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl, const SourceManager &SM)
For a given FunctionDecl returns the location where you would need to place the noexcept specifier.
Definition: LexerUtils.cpp:252