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