10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Expr.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Casting.h"
30 auto NegatedString = unaryOperator(
31 hasOperatorName(
"!"), hasUnaryOperand(ignoringImpCasts(stringLiteral())));
33 expr(anyOf(cxxBoolLiteral(equals(
false)), integerLiteral(equals(0)),
34 cxxNullPtrLiteralExpr(), gnuNullExpr(), NegatedString))
35 .bind(
"isAlwaysFalse");
36 auto IsAlwaysFalseWithCast = ignoringParenImpCasts(anyOf(
37 IsAlwaysFalse, cStyleCastExpr(has(ignoringParenImpCasts(IsAlwaysFalse)))
39 auto AssertExprRoot = anyOf(
41 hasAnyOperatorName(
"&&",
"=="),
42 hasEitherOperand(ignoringImpCasts(stringLiteral().bind(
"assertMSG"))),
43 anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
45 .bind(
"assertExprRoot"),
47 auto NonConstexprFunctionCall =
48 callExpr(hasDeclaration(functionDecl(unless(isConstexpr()))));
49 auto NonConstexprVariableReference =
50 declRefExpr(to(varDecl(unless(isConstexpr()))),
51 unless(hasAncestor(expr(matchers::hasUnevaluatedContext()))),
52 unless(hasAncestor(typeLoc())));
54 auto NonConstexprCode =
55 expr(anyOf(NonConstexprFunctionCall, NonConstexprVariableReference));
56 auto AssertCondition =
58 anyOf(expr(ignoringParenCasts(anyOf(
59 AssertExprRoot, unaryOperator(hasUnaryOperand(
60 ignoringParenCasts(AssertExprRoot)))))),
62 unless(NonConstexprCode), unless(hasDescendant(NonConstexprCode)))
65 anyOf(ignoringParenImpCasts(callExpr(
66 hasDeclaration(functionDecl(hasName(
"__builtin_expect"))),
67 hasArgument(0, AssertCondition))),
70 Finder->addMatcher(conditionalOperator(hasCondition(
Condition),
71 unless(isInTemplateInstantiation()))
76 ifStmt(hasCondition(
Condition), unless(isInTemplateInstantiation()))
82 const ASTContext *ASTCtx = Result.Context;
83 const LangOptions &Opts = ASTCtx->getLangOpts();
84 const SourceManager &SM = ASTCtx->getSourceManager();
85 const auto *CondStmt = Result.Nodes.getNodeAs<Stmt>(
"condStmt");
86 const auto *
Condition = Result.Nodes.getNodeAs<Expr>(
"condition");
87 const auto *IsAlwaysFalse = Result.Nodes.getNodeAs<Expr>(
"isAlwaysFalse");
88 const auto *AssertMSG = Result.Nodes.getNodeAs<StringLiteral>(
"assertMSG");
89 const auto *AssertExprRoot =
90 Result.Nodes.getNodeAs<BinaryOperator>(
"assertExprRoot");
91 const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>(
"castExpr");
92 SourceLocation AssertExpansionLoc = CondStmt->getBeginLoc();
94 if (!AssertExpansionLoc.isValid() || !AssertExpansionLoc.isMacroID())
98 Lexer::getImmediateMacroName(AssertExpansionLoc, SM, Opts);
106 if (IsAlwaysFalse && (!CastExpr || CastExpr->getType()->isPointerType())) {
107 SourceLocation FalseLiteralLoc =
108 SM.getImmediateSpellingLoc(IsAlwaysFalse->getExprLoc());
109 if (!FalseLiteralLoc.isMacroID())
112 StringRef FalseMacroName =
113 Lexer::getImmediateMacroName(FalseLiteralLoc, SM, Opts);
114 if (FalseMacroName.compare_insensitive(
"false") == 0 ||
115 FalseMacroName.compare_insensitive(
"null") == 0)
119 SourceLocation AssertLoc = SM.getImmediateMacroCallerLoc(AssertExpansionLoc);
121 SmallVector<FixItHint, 4> FixItHints;
122 SourceLocation LastParenLoc;
123 if (AssertLoc.isValid() && !AssertLoc.isMacroID() &&
124 (LastParenLoc = getLastParenLoc(ASTCtx, AssertLoc)).isValid()) {
125 FixItHints.push_back(
126 FixItHint::CreateReplacement(SourceRange(AssertLoc),
"static_assert"));
128 if (AssertExprRoot) {
129 FixItHints.push_back(FixItHint::CreateRemoval(
130 SourceRange(AssertExprRoot->getOperatorLoc())));
131 FixItHints.push_back(FixItHint::CreateRemoval(
132 SourceRange(AssertMSG->getBeginLoc(), AssertMSG->getEndLoc())));
133 FixItHints.push_back(FixItHint::CreateInsertion(
134 LastParenLoc, (Twine(
", \"") + AssertMSG->getString() +
"\"").str()));
135 }
else if (!Opts.CPlusPlus17) {
136 FixItHints.push_back(FixItHint::CreateInsertion(LastParenLoc,
", \"\""));
140 diag(AssertLoc,
"found assert() that could be replaced by static_assert()")
144SourceLocation StaticAssertCheck::getLastParenLoc(
const ASTContext *ASTCtx,
145 SourceLocation AssertLoc) {
146 const LangOptions &Opts = ASTCtx->getLangOpts();
147 const SourceManager &SM = ASTCtx->getSourceManager();
149 std::optional<llvm::MemoryBufferRef> Buffer =
150 SM.getBufferOrNone(SM.getFileID(AssertLoc));
154 const char *BufferPos = SM.getCharacterData(AssertLoc);
157 Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(AssertLoc)), Opts,
158 Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd());
161 if (Lexer.LexFromRawLexer(Token) || Lexer.LexFromRawLexer(Token) ||
162 !Token.is(tok::l_paren))
165 unsigned int ParenCount = 1;
166 while (ParenCount && !Lexer.LexFromRawLexer(Token)) {
167 if (Token.is(tok::l_paren))
169 else if (Token.is(tok::r_paren))
173 return Token.getLocation();
llvm::SmallString< 256U > Name
std::string Condition
Condition used after the preprocessor directive.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
StaticAssertCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.