10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
18 SourceLocation Loc1, SourceLocation Loc2) {
19 return Loc1.isFileID() && Loc2.isFileID() &&
20 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
24 return ND.isAnonymousNamespace() || ND.isInlineNamespace();
28 NamespaceDecl::decl_range Decls = ND.decls();
29 if (std::distance(Decls.begin(), Decls.end()) != 1)
32 const auto *ChildNamespace = dyn_cast<const NamespaceDecl>(*Decls.begin());
37 const SourceRange &ReplacementRange,
38 const SourceManager &Sources,
39 const LangOptions &LangOpts) {
41 CharSourceRange TextRange =
42 Lexer::getAsCharRange(ReplacementRange, Sources, LangOpts);
43 StringRef CurrentNamespacesText =
44 Lexer::getSourceText(TextRange, Sources, LangOpts);
45 return CurrentNamespacesText.count(
':') == (NumCandidates - 1) * 2;
48ConcatNestedNamespacesCheck::NamespaceString
49ConcatNestedNamespacesCheck::concatNamespaces() {
50 NamespaceString Result(
"namespace ");
51 Result.append(Namespaces.front()->getName());
53 std::for_each(std::next(Namespaces.begin()), Namespaces.end(),
54 [&Result](
const NamespaceDecl *ND) {
56 Result.append(ND->getName());
63 ast_matchers::MatchFinder *Finder) {
64 Finder->addMatcher(ast_matchers::namespaceDecl().bind(
"namespace"),
this);
67void ConcatNestedNamespacesCheck::reportDiagnostic(
68 const SourceRange &FrontReplacement,
const SourceRange &BackReplacement) {
69 diag(Namespaces.front()->getBeginLoc(),
70 "nested namespaces can be concatenated", DiagnosticIDs::Warning)
71 << FixItHint::CreateReplacement(FrontReplacement, concatNamespaces())
72 << FixItHint::CreateReplacement(BackReplacement,
"}");
76 const ast_matchers::MatchFinder::MatchResult &Result) {
77 const NamespaceDecl &ND = *Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
78 const SourceManager &Sources = *Result.SourceManager;
86 Namespaces.push_back(&ND);
91 SourceRange FrontReplacement(Namespaces.front()->getBeginLoc(),
92 Namespaces.back()->getLocation());
93 SourceRange BackReplacement(Namespaces.back()->getRBraceLoc(),
94 Namespaces.front()->getRBraceLoc());
98 reportDiagnostic(FrontReplacement, BackReplacement);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static bool alreadyConcatenated(std::size_t NumCandidates, const SourceRange &ReplacementRange, const SourceManager &Sources, const LangOptions &LangOpts)
static bool singleNamedNamespaceChild(const NamespaceDecl &ND)
static bool anonymousOrInlineNamespace(const NamespaceDecl &ND)
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)