Gabor Horvath | 5314521 | 2017-07-12 13:43:35 +0000 | [diff] [blame] | 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 | |
| 20 | void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) { |
Hans Wennborg | ef573fa | 2017-12-04 20:38:21 +0000 | [diff] [blame] | 21 | if (!getLangOpts().CPlusPlus17) |
Gabor Horvath | 5314521 | 2017-07-12 13:43:35 +0000 | [diff] [blame] | 22 | return; |
| 23 | |
| 24 | Finder->addMatcher(staticAssertDecl().bind("static_assert"), this); |
| 25 | } |
| 26 | |
| 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->getLocStart().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 |