Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- GoogleTidyModule.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 "GoogleTidyModule.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 | ExplicitConstructorCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { |
| 29 | Finder->addMatcher(constructorDecl().bind("construct"), this); |
| 30 | } |
| 31 | |
| 32 | void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) { |
| 33 | const CXXConstructorDecl *Ctor = |
| 34 | Result.Nodes.getNodeAs<CXXConstructorDecl>("construct"); |
| 35 | if (!Ctor->isExplicit() && !Ctor->isImplicit() && Ctor->getNumParams() >= 1 && |
| 36 | Ctor->getMinRequiredArguments() <= 1) { |
| 37 | SourceLocation Loc = Ctor->getLocation(); |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 38 | diag(Loc, "Single-argument constructors must be explicit") |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 39 | << FixItHint::CreateInsertion(Loc, "explicit "); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | class GoogleModule : public ClangTidyModule { |
| 44 | public: |
| 45 | virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) { |
| 46 | CheckFactories.addCheckFactory( |
| 47 | "google-explicit-constructor", |
| 48 | new ClangTidyCheckFactory<ExplicitConstructorCheck>()); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | // Register the GoogleTidyModule using this statically initialized variable. |
| 53 | static ClangTidyModuleRegistry::Add<GoogleModule> X("google-module", |
| 54 | "Adds Google lint checks."); |
| 55 | |
| 56 | // This anchor is used to force the linker to link in the generated object file |
| 57 | // and thus register the GoogleModule. |
| 58 | volatile int GoogleModuleAnchorSource = 0; |
| 59 | |
| 60 | } // namespace tidy |
| 61 | } // namespace clang |