Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame^] | 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 | |
| 15 | |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | using namespace clang::ast_matchers; |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | |
| 23 | NamespaceCommentCheck::NamespaceCommentCheck() |
| 24 | : NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" |
| 25 | "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$", |
| 26 | llvm::Regex::IgnoreCase), |
| 27 | ShortNamespaceLines(1) {} |
| 28 | |
| 29 | void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) { |
| 30 | Finder->addMatcher(namespaceDecl().bind("namespace"), this); |
| 31 | } |
| 32 | |
| 33 | bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, |
| 34 | SourceLocation Loc2) { |
| 35 | return Loc1.isFileID() && Loc2.isFileID() && |
| 36 | Sources.getFileID(Loc1) == Sources.getFileID(Loc2); |
| 37 | } |
| 38 | |
| 39 | std::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 | |
| 48 | void 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; |
| 74 | bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof); |
| 75 | |
| 76 | // Try to find existing namespace closing comment on the same line. |
| 77 | if (Tok.is(tok::comment) && NextTokenIsOnSameLine) { |
| 78 | StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength()); |
| 79 | SmallVector<StringRef, 6> Groups; |
| 80 | if (NamespaceCommentPattern.match(Comment, &Groups)) { |
| 81 | StringRef NamespaceNameInComment = Groups.size() >= 6 ? Groups[5] : ""; |
| 82 | |
| 83 | // Check if the namespace in the comment is the same. |
| 84 | if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) || |
| 85 | ND->getNameAsString() == NamespaceNameInComment) { |
| 86 | // FIXME: Maybe we need a strict mode, where we always fix namespace |
| 87 | // comments with different format. |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Otherwise we need to fix the comment. |
| 92 | NeedLineBreak = Comment.startswith("/*"); |
| 93 | CharSourceRange OldCommentRange = CharSourceRange::getCharRange( |
| 94 | SourceRange(Loc, Loc.getLocWithOffset(Tok.getLength()))); |
| 95 | diag(Loc, "namespace closing comment refers to a wrong namespace '%0'") |
| 96 | << NamespaceNameInComment |
| 97 | << FixItHint::CreateReplacement( |
| 98 | OldCommentRange, getNamespaceComment(ND, NeedLineBreak)); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // This is not a recognized form of a namespace closing comment. |
| 103 | // Leave line comment on the same line. Move block comment to the next line, |
| 104 | // as it can be multi-line or there may be other tokens behind it. |
| 105 | if (Comment.startswith("//")) |
| 106 | NeedLineBreak = false; |
| 107 | } |
| 108 | |
| 109 | diag(ND->getLocation(), "namespace not terminated with a closing comment") |
| 110 | << FixItHint::CreateInsertion( |
| 111 | AfterRBrace, " " + getNamespaceComment(ND, NeedLineBreak)); |
| 112 | } |
| 113 | |
| 114 | } // namespace tidy |
| 115 | } // namespace clang |