blob: 4ee6f993189f14cd2de9b3272348b123dfcb8571 [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
Alexander Kornienko32eaa372014-02-13 10:11:48 +000027void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
28 Finder->addMatcher(constructorDecl().bind("ctor"), this);
Daniel Jasperd07c8402013-07-29 08:19:24 +000029}
30
31void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
32 const CXXConstructorDecl *Ctor =
Alexander Kornienko32eaa372014-02-13 10:11:48 +000033 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 Jasperd07c8402013-07-29 08:19:24 +000043}
44
45class GoogleModule : public ClangTidyModule {
46public:
Alexander Kornienko21f3b772014-03-05 13:01:24 +000047 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
Daniel Jasperd07c8402013-07-29 08:19:24 +000048 CheckFactories.addCheckFactory(
49 "google-explicit-constructor",
50 new ClangTidyCheckFactory<ExplicitConstructorCheck>());
51 }
52};
53
54// Register the GoogleTidyModule using this statically initialized variable.
55static 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.
60volatile int GoogleModuleAnchorSource = 0;
61
62} // namespace tidy
63} // namespace clang