blob: 67c00630a2cd5afe30e0ef774d242f877e8663c7 [file] [log] [blame]
Gabor Horvath53145212017-07-12 13:43:35 +00001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace modernize {
19
20void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
Hans Wennborgef573fa2017-12-04 20:38:21 +000021 if (!getLangOpts().CPlusPlus17)
Gabor Horvath53145212017-07-12 13:43:35 +000022 return;
23
24 Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
25}
26
27void 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