clang-tools 19.0.0git
UnaryStaticAssertCheck.cpp
Go to the documentation of this file.
1//===--- UnaryStaticAssertCheck.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 "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::modernize {
16
18 Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
19}
20
21void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
22 const auto *MatchedDecl =
23 Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
24 const auto *AssertMessage =
25 dyn_cast_if_present<StringLiteral>(MatchedDecl->getMessage());
26
27 SourceLocation Loc = MatchedDecl->getLocation();
28
29 if (!AssertMessage || AssertMessage->getLength() ||
30 AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
31 return;
32
33 diag(Loc,
34 "use unary 'static_assert' when the string literal is an empty string")
35 << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
36}
37
38} // namespace clang::tidy::modernize
SourceLocation Loc
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.