blob: 05546052fb8f2497c4f7709baa24060c76c91734 [file] [log] [blame]
Haojian Wuc253f8b2016-04-05 11:42:08 +00001//===--- StaticDefinitionInAnonymousNamespaceCheck.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 "StaticDefinitionInAnonymousNamespaceCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20
Haojian Wuc253f8b2016-04-05 11:42:08 +000021void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers(
22 MatchFinder *Finder) {
Haojian Wuc242c8c2016-09-27 07:58:52 +000023 Finder->addMatcher(
24 namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
25 varDecl(isDefinition(), isStaticStorageClass())),
26 hasParent(namespaceDecl(isAnonymous())))
27 .bind("static-def"),
28 this);
Haojian Wuc253f8b2016-04-05 11:42:08 +000029}
30
31void StaticDefinitionInAnonymousNamespaceCheck::check(
32 const MatchFinder::MatchResult &Result) {
33 const auto *Def = Result.Nodes.getNodeAs<NamedDecl>("static-def");
34 // Skips all static definitions defined in Macro.
35 if (Def->getLocation().isMacroID())
36 return;
37
38 // Skips all static definitions in function scope.
39 const DeclContext *DC = Def->getDeclContext();
40 if (DC->getDeclKind() != Decl::Namespace)
41 return;
42
43 auto Diag =
44 diag(Def->getLocation(), "%0 is a static definition in "
45 "anonymous namespace; static is redundant here")
46 << Def;
47 Token Tok;
48 SourceLocation Loc = Def->getSourceRange().getBegin();
49 while (Loc < Def->getSourceRange().getEnd() &&
Gabor Horvathafad84c2016-09-24 02:13:45 +000050 !Lexer::getRawToken(Loc, Tok, *Result.SourceManager, getLangOpts(),
51 true)) {
Haojian Wuc253f8b2016-04-05 11:42:08 +000052 SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc());
Gabor Horvathafad84c2016-09-24 02:13:45 +000053 StringRef SourceText =
54 Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange),
55 *Result.SourceManager, getLangOpts());
Haojian Wuc253f8b2016-04-05 11:42:08 +000056 if (SourceText == "static") {
57 Diag << FixItHint::CreateRemoval(TokenRange);
58 break;
59 }
60 Loc = Tok.getEndLoc();
61 }
62}
63
64} // namespace readability
65} // namespace tidy
66} // namespace clang