clang-tools  11.0.0
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 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
18 
19 void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
20  Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
21 }
22 
23 void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
24  const auto *MatchedDecl =
25  Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
26  const StringLiteral *AssertMessage = MatchedDecl->getMessage();
27 
28  SourceLocation Loc = MatchedDecl->getLocation();
29 
30  if (!AssertMessage || AssertMessage->getLength() ||
31  AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
32  return;
33 
34  diag(Loc,
35  "use unary 'static_assert' when the string literal is an empty string")
36  << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
37 }
38 
39 } // namespace modernize
40 } // namespace tidy
41 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:37
UnaryStaticAssertCheck.h