blob: 8b25d0292e0e39efef717bf0cdf95a6309b967b1 [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- 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 Jasperd07c8402013-07-29 08:19:24 +000015#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +000016#include "clang/ASTMatchers/ASTMatchers.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000017#include "clang/Frontend/CompilerInstance.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000018#include "clang/Lex/PPCallbacks.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +000019#include "clang/Lex/Preprocessor.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000020#include "llvm/Support/raw_ostream.h"
21
22using namespace clang::ast_matchers;
23
24namespace clang {
25namespace tidy {
26
27void
28ExplicitConstructorCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
29 Finder->addMatcher(constructorDecl().bind("construct"), this);
30}
31
32void 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 Kornienko41bfe8d2014-01-13 10:50:51 +000038 diag(Loc, "Single-argument constructors must be explicit")
Daniel Jasperd07c8402013-07-29 08:19:24 +000039 << FixItHint::CreateInsertion(Loc, "explicit ");
40 }
41}
42
43class GoogleModule : public ClangTidyModule {
44public:
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.
53static 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.
58volatile int GoogleModuleAnchorSource = 0;
59
60} // namespace tidy
61} // namespace clang