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" |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 20 | namespace readability { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 21 | |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 22 | NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name, |
| 23 | ClangTidyContext *Context) |
| 24 | : ClangTidyCheck(Name, Context), |
| 25 | NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 26 | "namespace( +([a-zA-Z0-9_]+))?\\.? *(\\*/)?$", |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 27 | llvm::Regex::IgnoreCase), |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 28 | ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), |
Alexander Kornienko | 1efc425 | 2014-10-16 11:27:57 +0000 | [diff] [blame] | 29 | SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 30 | |
| 31 | void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 32 | Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); |
| 33 | Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments); |
| 34 | } |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 35 | |
| 36 | void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) { |
| 37 | Finder->addMatcher(namespaceDecl().bind("namespace"), this); |
| 38 | } |
| 39 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame^] | 40 | static bool locationsInSameFile(const SourceManager &Sources, |
| 41 | SourceLocation Loc1, SourceLocation Loc2) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 42 | return Loc1.isFileID() && Loc2.isFileID() && |
| 43 | Sources.getFileID(Loc1) == Sources.getFileID(Loc2); |
| 44 | } |
| 45 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame^] | 46 | static std::string getNamespaceComment(const NamespaceDecl *ND, |
| 47 | bool InsertLineBreak) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 48 | std::string Fix = "// namespace"; |
| 49 | if (!ND->isAnonymousNamespace()) |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 50 | Fix.append(" ").append(ND->getNameAsString()); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 51 | if (InsertLineBreak) |
| 52 | Fix.append("\n"); |
| 53 | return Fix; |
| 54 | } |
| 55 | |
| 56 | void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { |
| 57 | const NamespaceDecl *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace"); |
| 58 | const SourceManager &Sources = *Result.SourceManager; |
| 59 | |
| 60 | if (!locationsInSameFile(Sources, ND->getLocStart(), ND->getRBraceLoc())) |
| 61 | return; |
| 62 | |
| 63 | // Don't require closing comments for namespaces spanning less than certain |
| 64 | // number of lines. |
| 65 | unsigned StartLine = Sources.getSpellingLineNumber(ND->getLocStart()); |
| 66 | unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc()); |
| 67 | if (EndLine - StartLine + 1 <= ShortNamespaceLines) |
| 68 | return; |
| 69 | |
| 70 | // Find next token after the namespace closing brace. |
| 71 | SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1); |
| 72 | SourceLocation Loc = AfterRBrace; |
| 73 | Token Tok; |
| 74 | // Skip whitespace until we find the next token. |
| 75 | while (Lexer::getRawToken(Loc, Tok, Sources, Result.Context->getLangOpts())) { |
| 76 | Loc = Loc.getLocWithOffset(1); |
| 77 | } |
| 78 | if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc)) |
| 79 | return; |
| 80 | |
| 81 | bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine; |
Alexander Kornienko | 16ff5c7 | 2014-05-19 17:46:28 +0000 | [diff] [blame] | 82 | // If we insert a line comment before the token in the same line, we need |
| 83 | // to insert a line break. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 84 | bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof); |
| 85 | |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 86 | SourceRange OldCommentRange(AfterRBrace, AfterRBrace); |
Daniel Jasper | 08201e3 | 2015-03-05 23:17:32 +0000 | [diff] [blame] | 87 | std::string Message = "%0 not terminated with a closing comment"; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 88 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 89 | // Try to find existing namespace closing comment on the same line. |
| 90 | if (Tok.is(tok::comment) && NextTokenIsOnSameLine) { |
| 91 | StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength()); |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 92 | SmallVector<StringRef, 7> Groups; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 93 | if (NamespaceCommentPattern.match(Comment, &Groups)) { |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 94 | StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; |
| 95 | StringRef Anonymous = Groups.size() > 3 ? Groups[3] : ""; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 96 | |
| 97 | // Check if the namespace in the comment is the same. |
| 98 | if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) || |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 99 | (ND->getNameAsString() == NamespaceNameInComment && |
| 100 | Anonymous.empty())) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 101 | // FIXME: Maybe we need a strict mode, where we always fix namespace |
| 102 | // comments with different format. |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // Otherwise we need to fix the comment. |
| 107 | NeedLineBreak = Comment.startswith("/*"); |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 108 | OldCommentRange = |
| 109 | SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength())); |
| 110 | Message = |
| 111 | (llvm::Twine( |
| 112 | "%0 ends with a comment that refers to a wrong namespace '") + |
| 113 | NamespaceNameInComment + "'").str(); |
| 114 | } else if (Comment.startswith("//")) { |
| 115 | // Assume that this is an unrecognized form of a namespace closing line |
| 116 | // comment. Replace it. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 117 | NeedLineBreak = false; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 118 | OldCommentRange = |
| 119 | SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength())); |
| 120 | Message = "%0 ends with an unrecognized comment"; |
| 121 | } |
| 122 | // If it's a block comment, just move it to the next line, as it can be |
| 123 | // multi-line or there may be other tokens behind it. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 126 | std::string NamespaceName = |
| 127 | ND->isAnonymousNamespace() |
| 128 | ? "anonymous namespace" |
| 129 | : ("namespace '" + ND->getNameAsString() + "'"); |
| 130 | |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 131 | diag(AfterRBrace, Message) |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 132 | << NamespaceName |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 133 | << FixItHint::CreateReplacement( |
| 134 | OldCommentRange, std::string(SpacesBeforeComments, ' ') + |
| 135 | getNamespaceComment(ND, NeedLineBreak)); |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 136 | diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note) |
| 137 | << NamespaceName; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 140 | } // namespace readability |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 141 | } // namespace tidy |
| 142 | } // namespace clang |