10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 20 SourceLocation Loc1, SourceLocation Loc2) {
21 return Loc1.isFileID() && Loc2.isFileID() &&
22 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
26 return ND.isAnonymousNamespace() || ND.isInlineNamespace();
30 NamespaceDecl::decl_range Decls = ND.decls();
31 if (std::distance(Decls.begin(), Decls.end()) != 1)
34 const auto *ChildNamespace = dyn_cast<
const NamespaceDecl>(*Decls.begin());
39 const SourceRange &ReplacementRange,
40 const SourceManager &Sources,
41 const LangOptions &LangOpts) {
43 CharSourceRange TextRange =
44 Lexer::getAsCharRange(ReplacementRange, Sources, LangOpts);
45 StringRef CurrentNamespacesText =
46 Lexer::getSourceText(TextRange, Sources, LangOpts);
47 return CurrentNamespacesText.count(
':') == (NumCandidates - 1) * 2;
50 ConcatNestedNamespacesCheck::NamespaceString
51 ConcatNestedNamespacesCheck::concatNamespaces() {
52 NamespaceString Result(
"namespace ");
53 Result.append(Namespaces.front()->getName());
55 std::for_each(std::next(Namespaces.begin()), Namespaces.end(),
56 [&Result](
const NamespaceDecl *ND) {
58 Result.append(ND->getName());
65 ast_matchers::MatchFinder *Finder) {
69 Finder->addMatcher(ast_matchers::namespaceDecl().bind(
"namespace"),
this);
72 void ConcatNestedNamespacesCheck::reportDiagnostic(
73 const SourceRange &FrontReplacement,
const SourceRange &BackReplacement) {
74 diag(Namespaces.front()->getBeginLoc(),
75 "nested namespaces can be concatenated", DiagnosticIDs::Warning)
76 << FixItHint::CreateReplacement(FrontReplacement, concatNamespaces())
77 << FixItHint::CreateReplacement(BackReplacement,
"}");
81 const ast_matchers::MatchFinder::MatchResult &Result) {
82 const NamespaceDecl &ND = *Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
83 const SourceManager &Sources = *Result.SourceManager;
88 if (!Sources.isInMainFile(ND.getBeginLoc()))
94 Namespaces.push_back(&ND);
99 SourceRange FrontReplacement(Namespaces.front()->getBeginLoc(),
100 Namespaces.back()->getLocation());
101 SourceRange BackReplacement(Namespaces.back()->getRBraceLoc(),
102 Namespaces.front()->getRBraceLoc());
106 reportDiagnostic(FrontReplacement, BackReplacement);
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.
static bool alreadyConcatenated(std::size_t NumCandidates, const SourceRange &ReplacementRange, const SourceManager &Sources, const LangOptions &LangOpts)
static bool anonymousOrInlineNamespace(const NamespaceDecl &ND)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool singleNamedNamespaceChild(const NamespaceDecl &ND)
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.