1 //===--- UnaryStaticAssertCheck.cpp - clang-tidy---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnaryStaticAssertCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace modernize {
19 
registerMatchers(MatchFinder * Finder)20 void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
21   if (!getLangOpts().CPlusPlus17)
22     return;
23 
24   Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
25 }
26 
check(const MatchFinder::MatchResult & Result)27 void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
28   const auto *MatchedDecl =
29       Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
30   const StringLiteral *AssertMessage = MatchedDecl->getMessage();
31 
32   SourceLocation Loc = MatchedDecl->getLocation();
33 
34   if (!AssertMessage || AssertMessage->getLength() ||
35       AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
36     return;
37 
38   diag(Loc,
39        "use unary 'static_assert' when the string literal is an empty string")
40       << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
41 }
42 
43 } // namespace modernize
44 } // namespace tidy
45 } // namespace clang
46