Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- LLVMTidyModule.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 "LLVMTidyModule.h" |
| 11 | #include "../ClangTidy.h" |
| 12 | #include "../ClangTidyModule.h" |
| 13 | #include "../ClangTidyModuleRegistry.h" |
| 14 | #include "clang/AST/ASTContext.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 16 | #include "clang/ASTMatchers/ASTMatchers.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 18 | #include "clang/Lex/PPCallbacks.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace clang::ast_matchers; |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace tidy { |
| 26 | |
| 27 | void |
| 28 | NamespaceCommentCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { |
| 29 | Finder->addMatcher(namespaceDecl().bind("namespace"), this); |
| 30 | } |
| 31 | |
| 32 | void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { |
| 33 | const NamespaceDecl *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace"); |
| 34 | Token Tok; |
| 35 | SourceLocation Loc = ND->getRBraceLoc().getLocWithOffset(1); |
| 36 | while (Lexer::getRawToken(Loc, Tok, *Result.SourceManager, |
| 37 | Result.Context->getLangOpts())) { |
| 38 | Loc = Loc.getLocWithOffset(1); |
| 39 | } |
| 40 | // FIXME: Check that this namespace is "long". |
| 41 | if (Tok.is(tok::comment)) { |
| 42 | // FIXME: Check comment content. |
| 43 | return; |
| 44 | } |
| 45 | std::string Fix = " // namespace"; |
| 46 | if (!ND->isAnonymousNamespace()) |
| 47 | Fix = Fix.append(" ").append(ND->getNameAsString()); |
| 48 | |
| 49 | Context->Diag(ND->getLocation(), |
| 50 | "namespace not terminated with a closing comment") |
| 51 | << FixItHint::CreateInsertion(ND->getRBraceLoc().getLocWithOffset(1), |
| 52 | Fix); |
| 53 | } |
| 54 | |
| 55 | namespace { |
| 56 | class IncludeOrderPPCallbacks : public PPCallbacks { |
| 57 | public: |
| 58 | explicit IncludeOrderPPCallbacks(ClangTidyContext &Context) |
| 59 | : Context(Context) {} |
| 60 | |
| 61 | virtual void InclusionDirective(SourceLocation HashLoc, |
| 62 | const Token &IncludeTok, StringRef FileName, |
| 63 | bool IsAngled, CharSourceRange FilenameRange, |
| 64 | const FileEntry *File, StringRef SearchPath, |
| 65 | StringRef RelativePath, |
| 66 | const Module *Imported) { |
| 67 | // FIXME: This is a dummy implementation to show how to get at preprocessor |
| 68 | // information. Implement a real include order check. |
| 69 | Context.Diag(HashLoc, "This is an include"); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | ClangTidyContext &Context; |
| 74 | }; |
| 75 | } // namespace |
| 76 | |
| 77 | void IncludeOrderCheck::registerPPCallbacks(CompilerInstance &Compiler) { |
| 78 | Compiler.getPreprocessor() |
| 79 | .addPPCallbacks(new IncludeOrderPPCallbacks(*Context)); |
| 80 | } |
| 81 | |
| 82 | class LLVMModule : public ClangTidyModule { |
| 83 | public: |
| 84 | virtual ~LLVMModule() {} |
| 85 | |
| 86 | virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) { |
| 87 | CheckFactories.addCheckFactory( |
| 88 | "llvm-include-order", new ClangTidyCheckFactory<IncludeOrderCheck>()); |
| 89 | CheckFactories.addCheckFactory( |
| 90 | "llvm-namespace-comment", |
| 91 | new ClangTidyCheckFactory<NamespaceCommentCheck>()); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | // Register the LLVMTidyModule using this statically initialized variable. |
| 96 | static ClangTidyModuleRegistry::Add<LLVMModule> X("llvm-module", |
| 97 | "Adds LLVM lint checks."); |
| 98 | |
| 99 | // This anchor is used to force the linker to link in the generated object file |
| 100 | // and thus register the LLVMModule. |
| 101 | volatile int LLVMModuleAnchorSource = 0; |
| 102 | |
| 103 | } // namespace tidy |
| 104 | } // namespace clang |