From d07c840e6a125e954bfe015062c861369aae870e Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 29 Jul 2013 08:19:24 +0000 Subject: Initial architecture for clang-tidy. This is the first version of a possible clang-tidy architecture. The purpose of clang-tidy is to detect errors in adhering to common coding patterns, e.g. described in the LLVM Coding Standards. This is still heavily in flux. Review: http://llvm-reviews.chandlerc.com/D884 llvm-svn: 187345 --- .../clang-tidy/google/GoogleTidyModule.cpp | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp (limited to 'clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp') diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp new file mode 100644 index 00000000000..0839a06df55 --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp @@ -0,0 +1,61 @@ +//===--- GoogleTidyModule.cpp - clang-tidy --------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "GoogleTidyModule.h" +#include "../ClangTidy.h" +#include "../ClangTidyModule.h" +#include "../ClangTidyModuleRegistry.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/PPCallbacks.h" +#include "llvm/Support/raw_ostream.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void +ExplicitConstructorCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { + Finder->addMatcher(constructorDecl().bind("construct"), this); +} + +void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) { + const CXXConstructorDecl *Ctor = + Result.Nodes.getNodeAs("construct"); + if (!Ctor->isExplicit() && !Ctor->isImplicit() && Ctor->getNumParams() >= 1 && + Ctor->getMinRequiredArguments() <= 1) { + SourceLocation Loc = Ctor->getLocation(); + Context->Diag(Loc, "Single-argument constructors must be explicit") + << FixItHint::CreateInsertion(Loc, "explicit "); + } +} + +class GoogleModule : public ClangTidyModule { +public: + virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) { + CheckFactories.addCheckFactory( + "google-explicit-constructor", + new ClangTidyCheckFactory()); + } +}; + +// Register the GoogleTidyModule using this statically initialized variable. +static ClangTidyModuleRegistry::Add X("google-module", + "Adds Google lint checks."); + +// This anchor is used to force the linker to link in the generated object file +// and thus register the GoogleModule. +volatile int GoogleModuleAnchorSource = 0; + +} // namespace tidy +} // namespace clang -- cgit v1.2.3