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. |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 43 | // FIXME: Check comment placement on the same line. |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | std::string Fix = " // namespace"; |
| 47 | if (!ND->isAnonymousNamespace()) |
| 48 | Fix = Fix.append(" ").append(ND->getNameAsString()); |
| 49 | |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 50 | diag(ND->getLocation(), "namespace not terminated with a closing comment") |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 51 | << FixItHint::CreateInsertion(ND->getRBraceLoc().getLocWithOffset(1), |
| 52 | Fix); |
| 53 | } |
| 54 | |
| 55 | namespace { |
| 56 | class IncludeOrderPPCallbacks : public PPCallbacks { |
| 57 | public: |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 58 | explicit IncludeOrderPPCallbacks(IncludeOrderCheck &Check) |
| 59 | : Check(Check) {} |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 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. |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 69 | Check.diag(HashLoc, "This is an include"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private: |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 73 | IncludeOrderCheck &Check; |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 74 | }; |
| 75 | } // namespace |
| 76 | |
| 77 | void IncludeOrderCheck::registerPPCallbacks(CompilerInstance &Compiler) { |
| 78 | Compiler.getPreprocessor() |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame^] | 79 | .addPPCallbacks(new IncludeOrderPPCallbacks(*this)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 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 |