10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Basic/SourceLocation.h"
21 SourceLocation Loc1, SourceLocation Loc2) {
22 return Loc1.isFileID() && Loc2.isFileID() &&
23 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
27 const SourceManager &Sources,
28 const LangOptions &LangOpts) {
29 CharSourceRange TextRange = Lexer::getAsCharRange(
Range, Sources, LangOpts);
30 return Lexer::getSourceText(TextRange, Sources, LangOpts);
33std::optional<SourceRange>
35 const LangOptions &LangOpts)
const {
37 std::optional<Token> Tok =
39 back()->getLocation(), SM, LangOpts);
42 while (Tok->getKind() != tok::TokenKind::l_brace) {
48 return SourceRange{front()->getBeginLoc(), Tok->getEndLoc()};
51 return SourceRange{front()->getBeginLoc(), back()->getLocation()};
55 return SourceRange{front()->getRBraceLoc(), front()->getRBraceLoc()};
58 const LangOptions &LangOpts)
const {
60 SourceLocation
Loc = front()->getRBraceLoc();
61 std::optional<Token> Tok =
65 if (Tok->getKind() != tok::TokenKind::comment)
67 SourceRange TokRange = SourceRange{Tok->getLocation(), Tok->getEndLoc()};
73 constexpr size_t L =
sizeof(
"//") - 1U;
74 if (TokText.take_front(L) ==
"//" &&
75 TokText.drop_front(L).trim() != CloseComment)
77 return SourceRange{front()->getRBraceLoc(), Tok->getEndLoc()};
81 for (
const NamespaceDecl *ND : *
this) {
82 if (ND->isInlineNamespace())
83 Str.append(
"inline ");
84 Str.append(ND->getName());
98 if (ND.isAnonymousNamespace() || !ND.attrs().empty())
102 bool IsFirstNS = IsChild || !Namespaces.empty();
103 return ND.isInlineNamespace() && !IsFirstNS;
105 return ND.isInlineNamespace();
109 const NamespaceDecl &ND)
const {
110 NamespaceDecl::decl_range Decls = ND.decls();
111 if (std::distance(Decls.begin(), Decls.end()) != 1)
114 const auto *ChildNamespace = dyn_cast<const NamespaceDecl>(*Decls.begin());
119 ast_matchers::MatchFinder *Finder) {
120 Finder->addMatcher(ast_matchers::namespaceDecl().bind(
"namespace"),
this);
123void ConcatNestedNamespacesCheck::reportDiagnostic(
124 const SourceManager &SM,
const LangOptions &LangOpts) {
125 DiagnosticBuilder DB =
126 diag(Namespaces.front().front()->getBeginLoc(),
127 "nested namespaces can be concatenated", DiagnosticIDs::Warning);
129 SmallVector<SourceRange, 6> Fronts;
130 Fronts.reserve(Namespaces.size() - 1U);
131 SmallVector<SourceRange, 6> Backs;
132 Backs.reserve(Namespaces.size());
134 for (
const NS &ND : Namespaces) {
135 std::optional<SourceRange> SR =
136 ND.getCleanedNamespaceFrontRange(SM, LangOpts);
139 Fronts.push_back(SR.value());
140 Backs.push_back(ND.getNamespaceBackRange(SM, LangOpts));
142 if (Fronts.empty() || Backs.empty())
147 SourceRange LastRBrace = Backs.pop_back_val();
150 for (
const NS &NS : Namespaces) {
151 NS.appendName(ConcatNameSpace);
152 if (&NS != &Namespaces.back())
153 ConcatNameSpace.append(
"::");
156 for (SourceRange
const &Front : Fronts)
157 DB << FixItHint::CreateRemoval(Front);
158 DB << FixItHint::CreateReplacement(
159 Namespaces.back().getReplacedNamespaceFrontRange(), ConcatNameSpace);
160 if (LastRBrace != Namespaces.back().getDefaultNamespaceBackRange())
161 DB << FixItHint::CreateReplacement(LastRBrace,
162 (
"} // " + ConcatNameSpace).str());
163 for (SourceRange
const &Back : llvm::reverse(Backs))
164 DB << FixItHint::CreateRemoval(Back);
168 const ast_matchers::MatchFinder::MatchResult &Result) {
169 const NamespaceDecl &ND = *Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
170 const SourceManager &Sources = *Result.SourceManager;
179 Namespaces.push_back(
NS{});
180 if (!Namespaces.empty())
183 Namespaces.back().push_back(&ND);
185 if (singleNamedNamespaceChild(ND))
188 if (Namespaces.size() > 1)
189 reportDiagnostic(Sources, getLangOpts());
CharSourceRange Range
SourceRange for the file name.
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.
bool singleNamedNamespaceChild(const NamespaceDecl &ND) const
bool unsupportedNamespace(const NamespaceDecl &ND, bool IsChild) const
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.
SourceRange getDefaultNamespaceBackRange() const
void appendName(NamespaceName &Str) const
SourceRange getReplacedNamespaceFrontRange() const
std::optional< SourceRange > getCleanedNamespaceFrontRange(const SourceManager &SM, const LangOptions &LangOpts) const
SourceRange getNamespaceBackRange(const SourceManager &SM, const LangOptions &LangOpts) const
void appendCloseComment(NamespaceName &Str) const
static DeclarationName getName(const DependentScopeDeclRefExpr &D)
static StringRef getRawStringRef(const SourceRange &Range, const SourceManager &Sources, const LangOptions &LangOpts)
llvm::SmallString< 40 > NamespaceName
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
std::optional< Token > findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)