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 
9 #include "UnaryStaticAssertCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
18 
registerMatchers(MatchFinder * Finder)19 void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
20   if (!getLangOpts().CPlusPlus17)
21     return;
22 
23   Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
24 }
25 
check(const MatchFinder::MatchResult & Result)26 void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
27   const auto *MatchedDecl =
28       Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
29   const StringLiteral *AssertMessage = MatchedDecl->getMessage();
30 
31   SourceLocation Loc = MatchedDecl->getLocation();
32 
33   if (!AssertMessage || AssertMessage->getLength() ||
34       AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
35     return;
36 
37   diag(Loc,
38        "use unary 'static_assert' when the string literal is an empty string")
39       << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
40 }
41 
42 } // namespace modernize
43 } // namespace tidy
44 } // namespace clang
45