blob: d9789fe2dda5a21c73cbc7758f71c6c3f51232e0 [file] [log] [blame]
Alexander Kornienkobef51cd2014-05-19 16:39:08 +00001//===--- 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
15
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang::ast_matchers;
19
20namespace clang {
21namespace tidy {
22
23NamespaceCommentCheck::NamespaceCommentCheck()
24 : NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
25 "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$",
26 llvm::Regex::IgnoreCase),
27 ShortNamespaceLines(1) {}
28
29void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
30 Finder->addMatcher(namespaceDecl().bind("namespace"), this);
31}
32
33bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1,
34 SourceLocation Loc2) {
35 return Loc1.isFileID() && Loc2.isFileID() &&
36 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
37}
38
39std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak) {
40 std::string Fix = "// namespace";
41 if (!ND->isAnonymousNamespace())
42 Fix.append(" ").append(ND->getNameAsString());
43 if (InsertLineBreak)
44 Fix.append("\n");
45 return Fix;
46}
47
48void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
49 const NamespaceDecl *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace");
50 const SourceManager &Sources = *Result.SourceManager;
51
52 if (!locationsInSameFile(Sources, ND->getLocStart(), ND->getRBraceLoc()))
53 return;
54
55 // Don't require closing comments for namespaces spanning less than certain
56 // number of lines.
57 unsigned StartLine = Sources.getSpellingLineNumber(ND->getLocStart());
58 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
59 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
60 return;
61
62 // Find next token after the namespace closing brace.
63 SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1);
64 SourceLocation Loc = AfterRBrace;
65 Token Tok;
66 // Skip whitespace until we find the next token.
67 while (Lexer::getRawToken(Loc, Tok, Sources, Result.Context->getLangOpts())) {
68 Loc = Loc.getLocWithOffset(1);
69 }
70 if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
71 return;
72
73 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
Alexander Kornienko16ff5c72014-05-19 17:46:28 +000074 // If we insert a line comment before the token in the same line, we need
75 // to insert a line break.
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000076 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
77
78 // Try to find existing namespace closing comment on the same line.
79 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
80 StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
81 SmallVector<StringRef, 6> Groups;
82 if (NamespaceCommentPattern.match(Comment, &Groups)) {
83 StringRef NamespaceNameInComment = Groups.size() >= 6 ? Groups[5] : "";
84
85 // Check if the namespace in the comment is the same.
86 if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
87 ND->getNameAsString() == NamespaceNameInComment) {
88 // FIXME: Maybe we need a strict mode, where we always fix namespace
89 // comments with different format.
90 return;
91 }
92
93 // Otherwise we need to fix the comment.
94 NeedLineBreak = Comment.startswith("/*");
95 CharSourceRange OldCommentRange = CharSourceRange::getCharRange(
96 SourceRange(Loc, Loc.getLocWithOffset(Tok.getLength())));
97 diag(Loc, "namespace closing comment refers to a wrong namespace '%0'")
98 << NamespaceNameInComment
99 << FixItHint::CreateReplacement(
100 OldCommentRange, getNamespaceComment(ND, NeedLineBreak));
101 return;
102 }
103
104 // This is not a recognized form of a namespace closing comment.
105 // Leave line comment on the same line. Move block comment to the next line,
106 // as it can be multi-line or there may be other tokens behind it.
107 if (Comment.startswith("//"))
108 NeedLineBreak = false;
109 }
110
111 diag(ND->getLocation(), "namespace not terminated with a closing comment")
112 << FixItHint::CreateInsertion(
113 AfterRBrace, " " + getNamespaceComment(ND, NeedLineBreak));
114}
115
116} // namespace tidy
117} // namespace clang