clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

NamespaceCommentCheck.cpp
Go to the documentation of this file.
1 //===--- NamespaceCommentCheck.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 "NamespaceCommentCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Lex/Lexer.h"
14 #include "llvm/ADT/StringExtras.h"
15
16 using namespace clang::ast_matchers;
17
18 namespace clang {
19 namespace tidy {
20 namespace readability {
21
22 NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
23 ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context),
25 NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
26 "namespace( +([a-zA-Z0-9_]+))?\\.? *(\\*/)?$",
27 llvm::Regex::IgnoreCase),
28 ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)),
29 SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {}
30
31 void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
32 Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
33 Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
34 }
35
36 void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
37 // Only register the matchers for C++; the functionality currently does not
38 // provide any benefit to other languages, despite being benign.
39 if (getLangOpts().CPlusPlus)
40 Finder->addMatcher(namespaceDecl().bind("namespace"), this);
41 }
42
43 static bool locationsInSameFile(const SourceManager &Sources,
44 SourceLocation Loc1, SourceLocation Loc2) {
45 return Loc1.isFileID() && Loc2.isFileID() &&
46 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
47 }
48
49 static std::string getNamespaceComment(const NamespaceDecl *ND,
50 bool InsertLineBreak) {
51 std::string Fix = "// namespace";
52 if (!ND->isAnonymousNamespace())
53 Fix.append(" ").append(ND->getNameAsString());
54 if (InsertLineBreak)
55 Fix.append("\n");
56 return Fix;
57 }
58
59 void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
60 const NamespaceDecl *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace");
61 const SourceManager &Sources = *Result.SourceManager;
62
63 if (!locationsInSameFile(Sources, ND->getLocStart(), ND->getRBraceLoc()))
64 return;
65
66 // Don't require closing comments for namespaces spanning less than certain
67 // number of lines.
68 unsigned StartLine = Sources.getSpellingLineNumber(ND->getLocStart());
69 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
70 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
71 return;
72
73 // Find next token after the namespace closing brace.
74 SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1);
75 SourceLocation Loc = AfterRBrace;
76 Token Tok;
77 // Skip whitespace until we find the next token.
78 while (Lexer::getRawToken(Loc, Tok, Sources, Result.Context->getLangOpts()) ||
79 Tok.is(tok::semi)) {
80 Loc = Loc.getLocWithOffset(1);
81 }
82 if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
83 return;
84
85 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
86 // If we insert a line comment before the token in the same line, we need
87 // to insert a line break.
88 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
89
90 SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
91 std::string Message = "%0 not terminated with a closing comment";
92
93 // Try to find existing namespace closing comment on the same line.
94 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
95 StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
96 SmallVector<StringRef, 7> Groups;
97 if (NamespaceCommentPattern.match(Comment, &Groups)) {
98 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
99 StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
100
101 // Check if the namespace in the comment is the same.
102 if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
103 (ND->getNameAsString() == NamespaceNameInComment &&
104 Anonymous.empty())) {
105 // FIXME: Maybe we need a strict mode, where we always fix namespace
106 // comments with different format.
107 return;
108 }
109
110 // Otherwise we need to fix the comment.
111 NeedLineBreak = Comment.startswith("/*");
112 OldCommentRange =
113 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
114 Message =
115 (llvm::Twine(
116 "%0 ends with a comment that refers to a wrong namespace '") +
117 NamespaceNameInComment + "'").str();
118 } else if (Comment.startswith("//")) {
119 // Assume that this is an unrecognized form of a namespace closing line
120 // comment. Replace it.
121 NeedLineBreak = false;
122 OldCommentRange =
123 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
124 Message = "%0 ends with an unrecognized comment";
125 }
126 // If it's a block comment, just move it to the next line, as it can be
127 // multi-line or there may be other tokens behind it.
128 }
129
130 std::string NamespaceName =
131 ND->isAnonymousNamespace()
132 ? "anonymous namespace"
133 : ("namespace '" + ND->getNameAsString() + "'");
134
135 diag(AfterRBrace, Message)
136 << NamespaceName << FixItHint::CreateReplacement(
137 CharSourceRange::getCharRange(OldCommentRange),
138 std::string(SpacesBeforeComments, ' ') +
139 getNamespaceComment(ND, NeedLineBreak));
140 diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note)
141 << NamespaceName;
142 }
143
144 } // namespace readability
145 } // namespace tidy
146 } // namespace clang
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:34
clang::tidy::readability::NamespaceCommentCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
Definition: NamespaceCommentCheck.cpp:36
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:162
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::readability::NamespaceCommentCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NamespaceCommentCheck.cpp:59
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:158
clang::tidy::readability::locationsInSameFile
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)
Definition: NamespaceCommentCheck.cpp:43
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:344
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
NamespaceCommentCheck.h
clang::tidy::readability::getNamespaceComment
static std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak)
Definition: NamespaceCommentCheck.cpp:49
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
Fix
static cl::opt< bool > Fix("fix", cl::desc("Apply suggested fixes. Without -fix-errors\n""clang-tidy will bail out if any compilation\n""errors were found."), cl::init(false), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121