Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,220 @@
//===--- AvoidCStyleCastsCheck.cpp - clang-tidy -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AvoidCStyleCastsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace readability {
void AvoidCStyleCastsCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
Finder->addMatcher(
cStyleCastExpr(
// Filter out (EnumType)IntegerLiteral construct, which is generated
// for non-type template arguments of enum types.
// FIXME: Remove this once this is fixed in the AST.
unless(hasParent(substNonTypeTemplateParmExpr())),
// Avoid matches in template instantiations.
unless(isInTemplateInstantiation()))
.bind("cast"),
this);
}
static bool needsConstCast(QualType SourceType, QualType DestType) {
while ((SourceType->isPointerType() && DestType->isPointerType()) ||
(SourceType->isReferenceType() && DestType->isReferenceType())) {
SourceType = SourceType->getPointeeType();
DestType = DestType->getPointeeType();
if (SourceType.isConstQualified() && !DestType.isConstQualified()) {
return (SourceType->isPointerType() == DestType->isPointerType()) &&
(SourceType->isReferenceType() == DestType->isReferenceType());
}
}
return false;
}
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2) {
while ((T1->isPointerType() && T2->isPointerType()) ||
(T1->isReferenceType() && T2->isReferenceType())) {
T1 = T1->getPointeeType();
T2 = T2->getPointeeType();
}
return T1.getUnqualifiedType() == T2.getUnqualifiedType();
}
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
// Ignore casts in macros.
if (CastExpr->getExprLoc().isMacroID())
return;
// Casting to void is an idiomatic way to mute "unused variable" and similar
// warnings.
if (CastExpr->getCastKind() == CK_ToVoid)
return;
auto isFunction = [](QualType T) {
T = T.getCanonicalType().getNonReferenceType();
return T->isFunctionType() || T->isFunctionPointerType() ||
T->isMemberFunctionPointerType();
};
const QualType DestTypeAsWritten =
CastExpr->getTypeAsWritten().getUnqualifiedType();
const QualType SourceTypeAsWritten =
CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
const QualType DestType = DestTypeAsWritten.getCanonicalType();
auto ReplaceRange = CharSourceRange::getCharRange(
CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getLocStart());
bool FnToFnCast =
isFunction(SourceTypeAsWritten) && isFunction(DestTypeAsWritten);
if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
// Function pointer/reference casts may be needed to resolve ambiguities in
// case of overloaded functions, so detection of redundant casts is trickier
// in this case. Don't emit "redundant cast" warnings for function
// pointer/reference types.
if (SourceTypeAsWritten == DestTypeAsWritten) {
diag(CastExpr->getLocStart(), "redundant cast to the same type")
<< FixItHint::CreateRemoval(ReplaceRange);
return;
}
}
// The rest of this check is only relevant to C++.
if (!getLangOpts().CPlusPlus)
return;
// Ignore code inside extern "C" {} blocks.
if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
.empty())
return;
// Ignore code in .c files and headers included from them, even if they are
// compiled as C++.
if (getCurrentMainFile().endswith(".c"))
return;
SourceManager &SM = *Result.SourceManager;
// Ignore code in .c files #included in other files (which shouldn't be done,
// but people still do this for test and other purposes).
if (SM.getFilename(SM.getSpellingLoc(CastExpr->getLocStart())).endswith(".c"))
return;
// Leave type spelling exactly as it was (unlike
// getTypeAsWritten().getAsString() which would spell enum types 'enum X').
StringRef DestTypeString =
Lexer::getSourceText(CharSourceRange::getTokenRange(
CastExpr->getLParenLoc().getLocWithOffset(1),
CastExpr->getRParenLoc().getLocWithOffset(-1)),
SM, getLangOpts());
auto Diag =
diag(CastExpr->getLocStart(), "C-style casts are discouraged; use %0");
auto ReplaceWithCast = [&](std::string CastText) {
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
if (!isa<ParenExpr>(SubExpr)) {
CastText.push_back('(');
Diag << FixItHint::CreateInsertion(
Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0, SM,
getLangOpts()),
")");
}
Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
};
auto ReplaceWithNamedCast = [&](StringRef CastType) {
Diag << CastType;
ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
};
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
switch (CastExpr->getCastKind()) {
case CK_FunctionToPointerDecay:
ReplaceWithNamedCast("static_cast");
return;
case CK_ConstructorConversion:
if (!CastExpr->getTypeAsWritten().hasQualifiers() &&
DestTypeAsWritten->isRecordType() &&
!DestTypeAsWritten->isElaboratedTypeSpecifier()) {
Diag << "constructor call syntax";
// FIXME: Validate DestTypeString, maybe.
ReplaceWithCast(DestTypeString.str());
} else {
ReplaceWithNamedCast("static_cast");
}
return;
case CK_NoOp:
if (FnToFnCast) {
ReplaceWithNamedCast("static_cast");
return;
}
if (SourceType == DestType) {
Diag << "static_cast (if needed, the cast may be redundant)";
ReplaceWithCast(("static_cast<" + DestTypeString + ">").str());
return;
}
if (needsConstCast(SourceType, DestType) &&
pointedUnqualifiedTypesAreEqual(SourceType, DestType)) {
ReplaceWithNamedCast("const_cast");
return;
}
if (DestType->isReferenceType()) {
QualType Dest = DestType.getNonReferenceType();
QualType Source = SourceType.getNonReferenceType();
if (Source == Dest.withConst() ||
SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
ReplaceWithNamedCast("const_cast");
return;
}
break;
}
// FALLTHROUGH
case clang::CK_IntegralCast:
// Convert integral and no-op casts between builtin types and enums to
// static_cast. A cast from enum to integer may be unnecessary, but it's
// still retained.
if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
(DestType->isBuiltinType() || DestType->isEnumeralType())) {
ReplaceWithNamedCast("static_cast");
return;
}
break;
case CK_BitCast:
// FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
if (!needsConstCast(SourceType, DestType)) {
if (SourceType->isVoidPointerType())
ReplaceWithNamedCast("static_cast");
else
ReplaceWithNamedCast("reinterpret_cast");
return;
}
break;
default:
break;
}
Diag << "static_cast/const_cast/reinterpret_cast";
}
} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,42 @@
//===--- AvoidCStyleCastsCheck.h - clang-tidy -------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace readability {
/// Finds usages of C-style casts.
///
/// https://google.github.io/styleguide/cppguide.html#Casting
///
/// Corresponding cpplint.py check name: 'readability/casting'.
///
/// This check is similar to `-Wold-style-cast`, but it suggests automated fixes
/// in some cases. The reported locations should not be different from the
/// ones generated by `-Wold-style-cast`.
class AvoidCStyleCastsCheck : public ClangTidyCheck {
public:
AvoidCStyleCastsCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H

View File

@@ -0,0 +1,47 @@
//===--- AvoidThrowingObjCExceptionCheck.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 "AvoidThrowingObjCExceptionCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace objc {
void AvoidThrowingObjCExceptionCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this);
Finder->addMatcher(
objcMessageExpr(anyOf(hasSelector("raise:format:"),
hasSelector("raise:format:arguments:")),
hasReceiverType(asString("NSException")))
.bind("raiseException"),
this);
}
void AvoidThrowingObjCExceptionCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *MatchedStmt =
Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt");
const auto *MatchedExpr =
Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException");
auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc()
: MatchedStmt->getThrowLoc();
diag(SourceLoc,
"pass in NSError ** instead of throwing exception to indicate "
"Objective-C errors");
}
} // namespace objc
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,39 @@
//===--- AvoidThrowingObjCExceptionCheck.h - clang-tidy----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace objc {
/// The check is to find usage of @throw invocation in Objective-C code.
/// We should avoid using @throw for Objective-C exceptions according to
/// the Google Objective-C Style Guide.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/google-objc-avoid-throwing-exception.html
class AvoidThrowingObjCExceptionCheck : public ClangTidyCheck {
public:
AvoidThrowingObjCExceptionCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace objc
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H

View File

@@ -0,0 +1,28 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyGoogleModule
AvoidCStyleCastsCheck.cpp
AvoidThrowingObjCExceptionCheck.cpp
DefaultArgumentsCheck.cpp
ExplicitConstructorCheck.cpp
ExplicitMakePairCheck.cpp
GlobalNamesInHeadersCheck.cpp
GlobalVariableDeclarationCheck.cpp
GoogleTidyModule.cpp
IntegerTypesCheck.cpp
NonConstReferences.cpp
OverloadedUnaryAndCheck.cpp
StringReferenceMemberCheck.cpp
TodoCommentCheck.cpp
UnnamedNamespaceInHeaderCheck.cpp
UsingNamespaceDirectiveCheck.cpp
LINK_LIBS
clangAST
clangASTMatchers
clangBasic
clangLex
clangTidy
clangTidyReadabilityModule
clangTidyUtils
)

View File

@@ -0,0 +1,36 @@
//===--- DefaultArgumentsCheck.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 "DefaultArgumentsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
void DefaultArgumentsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxMethodDecl(anyOf(isOverride(), isVirtual()),
hasAnyParameter(parmVarDecl(hasInitializer(expr()))))
.bind("Decl"),
this);
}
void DefaultArgumentsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("Decl");
diag(MatchedDecl->getLocation(),
"default arguments on virtual or override methods are prohibited");
}
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,34 @@
//===--- DefaultArgumentsCheck.h - clang-tidy--------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
/// Checks that default parameters are not given for virtual methods.
///
/// See https://google.github.io/styleguide/cppguide.html#Default_Arguments
class DefaultArgumentsCheck : public ClangTidyCheck {
public:
DefaultArgumentsCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H

View File

@@ -0,0 +1,156 @@
//===--- ExplicitConstructorCheck.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 "ExplicitConstructorCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
// provide any benefit to other languages, despite being benign.
if (!getLangOpts().CPlusPlus)
return;
Finder->addMatcher(
cxxConstructorDecl(unless(anyOf(isImplicit(), // Compiler-generated.
isDeleted(), isInstantiated())))
.bind("ctor"),
this);
Finder->addMatcher(
cxxConversionDecl(unless(anyOf(isExplicit(), // Already marked explicit.
isImplicit(), // Compiler-generated.
isDeleted(), isInstantiated())))
.bind("conversion"),
this);
}
// Looks for the token matching the predicate and returns the range of the found
// token including trailing whitespace.
static SourceRange FindToken(const SourceManager &Sources,
const LangOptions &LangOpts,
SourceLocation StartLoc, SourceLocation EndLoc,
bool (*Pred)(const Token &)) {
if (StartLoc.isMacroID() || EndLoc.isMacroID())
return SourceRange();
FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
StringRef Buf = Sources.getBufferData(File);
const char *StartChar = Sources.getCharacterData(StartLoc);
Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
Lex.SetCommentRetentionState(true);
Token Tok;
do {
Lex.LexFromRawLexer(Tok);
if (Pred(Tok)) {
Token NextTok;
Lex.LexFromRawLexer(NextTok);
return SourceRange(Tok.getLocation(), NextTok.getLocation());
}
} while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
return SourceRange();
}
static bool declIsStdInitializerList(const NamedDecl *D) {
// First use the fast getName() method to avoid unnecessary calls to the
// slow getQualifiedNameAsString().
return D->getName() == "initializer_list" &&
D->getQualifiedNameAsString() == "std::initializer_list";
}
static bool isStdInitializerList(QualType Type) {
Type = Type.getCanonicalType();
if (const auto *TS = Type->getAs<TemplateSpecializationType>()) {
if (const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
return declIsStdInitializerList(TD);
}
if (const auto *RT = Type->getAs<RecordType>()) {
if (const auto *Specialization =
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
return declIsStdInitializerList(Specialization->getSpecializedTemplate());
}
return false;
}
void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
constexpr char WarningMessage[] =
"%0 must be marked explicit to avoid unintentional implicit conversions";
if (const auto *Conversion =
Result.Nodes.getNodeAs<CXXConversionDecl>("conversion")) {
if (Conversion->isOutOfLine())
return;
SourceLocation Loc = Conversion->getLocation();
// Ignore all macros until we learn to ignore specific ones (e.g. used in
// gmock to define matchers).
if (Loc.isMacroID())
return;
diag(Loc, WarningMessage)
<< Conversion << FixItHint::CreateInsertion(Loc, "explicit ");
return;
}
const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
if (Ctor->isOutOfLine() || Ctor->getNumParams() == 0 ||
Ctor->getMinRequiredArguments() > 1)
return;
bool takesInitializerList = isStdInitializerList(
Ctor->getParamDecl(0)->getType().getNonReferenceType());
if (Ctor->isExplicit() &&
(Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
auto isKWExplicit = [](const Token &Tok) {
return Tok.is(tok::raw_identifier) &&
Tok.getRawIdentifier() == "explicit";
};
SourceRange ExplicitTokenRange =
FindToken(*Result.SourceManager, getLangOpts(),
Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
StringRef ConstructorDescription;
if (Ctor->isMoveConstructor())
ConstructorDescription = "move";
else if (Ctor->isCopyConstructor())
ConstructorDescription = "copy";
else
ConstructorDescription = "initializer-list";
auto Diag = diag(Ctor->getLocation(),
"%0 constructor should not be declared explicit")
<< ConstructorDescription;
if (ExplicitTokenRange.isValid()) {
Diag << FixItHint::CreateRemoval(
CharSourceRange::getCharRange(ExplicitTokenRange));
}
return;
}
if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
takesInitializerList)
return;
bool SingleArgument =
Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
SourceLocation Loc = Ctor->getLocation();
diag(Loc, WarningMessage)
<< (SingleArgument
? "single-argument constructors"
: "constructors that are callable with a single argument")
<< FixItHint::CreateInsertion(Loc, "explicit ");
}
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,34 @@
//===--- ExplicitConstructorCheck.h - clang-tidy ----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITCONSTRUCTORCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITCONSTRUCTORCHECK_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
/// Checks that all single-argument constructors are explicit.
///
/// See https://google.github.io/styleguide/cppguide.html#Explicit_Constructors
class ExplicitConstructorCheck : public ClangTidyCheck {
public:
ExplicitConstructorCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITCONSTRUCTORCHECK_H

View File

@@ -0,0 +1,78 @@
//===--- ExplicitMakePairCheck.cpp - clang-tidy -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ExplicitMakePairCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang {
namespace {
AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
return Node.hasExplicitTemplateArgs();
}
} // namespace
namespace tidy {
namespace google {
namespace build {
void ExplicitMakePairCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
// provide any benefit to other languages, despite being benign.
if (!getLangOpts().CPlusPlus)
return;
// Look for std::make_pair with explicit template args. Ignore calls in
// templates.
Finder->addMatcher(
callExpr(unless(isInTemplateInstantiation()),
callee(expr(ignoringParenImpCasts(
declRefExpr(hasExplicitTemplateArgs(),
to(functionDecl(hasName("::std::make_pair"))))
.bind("declref")))))
.bind("call"),
this);
}
void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declref");
// Sanity check: The use might have overriden ::std::make_pair.
if (Call->getNumArgs() != 2)
return;
const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts();
const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts();
// If types don't match, we suggest replacing with std::pair and explicit
// template arguments. Otherwise just remove the template arguments from
// make_pair.
if (Arg0->getType() != Call->getArg(0)->getType() ||
Arg1->getType() != Call->getArg(1)->getType()) {
diag(Call->getLocStart(), "for C++11-compatibility, use pair directly")
<< FixItHint::CreateReplacement(
SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()),
"std::pair<");
} else {
diag(Call->getLocStart(),
"for C++11-compatibility, omit template arguments from make_pair")
<< FixItHint::CreateRemoval(
SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc()));
}
}
} // namespace build
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,39 @@
//===--- ExplicitMakePairCheck.h - clang-tidy -------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITMAKEPAIRCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITMAKEPAIRCHECK_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace build {
/// Check that `make_pair`'s template arguments are deduced.
///
/// G++ 4.6 in C++11 mode fails badly if `make_pair`'s template arguments are
/// specified explicitly, and such use isn't intended in any case.
///
/// Corresponding cpplint.py check name: 'build/explicit_make_pair'.
class ExplicitMakePairCheck : public ClangTidyCheck {
public:
ExplicitMakePairCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace build
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICITMAKEPAIRCHECK_H

View File

@@ -0,0 +1,80 @@
//===--- GlobalNamesInHeadersCheck.cpp - clang-tidy -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "GlobalNamesInHeadersCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace readability {
GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
"HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
HeaderFileExtensions, ',')) {
llvm::errs() << "Invalid header file extension: "
<< RawStringHeaderFileExtensions << "\n";
}
}
void GlobalNamesInHeadersCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
}
void GlobalNamesInHeadersCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
Finder->addMatcher(decl(anyOf(usingDecl(), usingDirectiveDecl()),
hasDeclContext(translationUnitDecl()))
.bind("using_decl"),
this);
}
void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
// If it comes from a macro, we'll assume it is fine.
if (D->getLocStart().isMacroID())
return;
// Ignore if it comes from the "main" file ...
if (Result.SourceManager->isInMainFile(
Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
// unless that file is a header.
if (!utils::isSpellingLocInHeaderFile(
D->getLocStart(), *Result.SourceManager, HeaderFileExtensions))
return;
}
if (const auto *UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
// Anynoumous namespaces inject a using directive into the AST to import
// the names into the containing namespace.
// We should not have them in headers, but there is another warning for
// that.
return;
}
}
diag(D->getLocStart(),
"using declarations in the global namespace in headers are prohibited");
}
} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,47 @@
//===--- GlobalNamesInHeadersCheck.h - clang-tidy ---------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBALNAMESINHEADERSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBALNAMESINHEADERSCHECK_H
#include "../ClangTidy.h"
#include "../utils/HeaderFileExtensionsUtils.h"
namespace clang {
namespace tidy {
namespace google {
namespace readability {
/// Flag global namespace pollution in header files.
/// Right now it only triggers on using declarations and directives.
///
/// The check supports these options:
/// - `HeaderFileExtensions`: a comma-separated list of filename extensions
/// of header files (the filename extensions should not contain "." prefix).
/// "h" by default.
/// For extension-less header files, using an empty string or leaving an
/// empty string between "," if there are other filename extensions.
class GlobalNamesInHeadersCheck : public ClangTidyCheck {
public:
GlobalNamesInHeadersCheck(StringRef Name, ClangTidyContext *Context);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
const std::string RawStringHeaderFileExtensions;
utils::HeaderFileExtensionsSet HeaderFileExtensions;
};
} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBALNAMESINHEADERSCHECK_H

View File

@@ -0,0 +1,90 @@
//===--- GlobalVariableDeclarationCheck.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 "GlobalVariableDeclarationCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include <string>
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace objc {
namespace {
FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
char FC = Decl->getName()[0];
if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
// No fix available if first character is not alphabetical character, or it
// is a single-character variable, since it is difficult to determine the
// proper fix in this case. Users should create a proper variable name by
// their own.
return FixItHint();
}
char SC = Decl->getName()[1];
if ((FC == 'k' || FC == 'g') && !llvm::isAlpha(SC)) {
// No fix available if the prefix is correct but the second character is not
// alphabetical, since it is difficult to determine the proper fix in this
// case.
return FixItHint();
}
auto NewName = (IsConst ? "k" : "g") +
llvm::StringRef(std::string(1, FC)).upper() +
Decl->getName().substr(1).str();
return FixItHint::CreateReplacement(
CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
llvm::StringRef(NewName));
}
} // namespace
void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) {
// The relevant Style Guide rule only applies to Objective-C.
if (!getLangOpts().ObjC1 && !getLangOpts().ObjC2) {
return;
}
// need to add two matchers since we need to bind different ids to distinguish
// constants and variables. Since bind() can only be called on node matchers,
// we cannot make it in one matcher.
Finder->addMatcher(
varDecl(hasGlobalStorage(), unless(hasType(isConstQualified())),
unless(matchesName("::g[A-Z]")))
.bind("global_var"),
this);
Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
unless(matchesName("::k[A-Z]")))
.bind("global_const"),
this);
}
void GlobalVariableDeclarationCheck::check(
const MatchFinder::MatchResult &Result) {
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_var")) {
diag(Decl->getLocation(),
"non-const global variable '%0' must have a name which starts with "
"'g[A-Z]'")
<< Decl->getName() << generateFixItHint(Decl, false);
}
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_const")) {
diag(Decl->getLocation(),
"const global variable '%0' must have a name which starts with "
"'k[A-Z]'")
<< Decl->getName() << generateFixItHint(Decl, true);
}
}
} // namespace objc
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,39 @@
//===--- GlobalVariableDeclarationCheck.h - clang-tidy-----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_GLOBAL_VARIABLE_DECLARATION_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_GLOBAL_VARIABLE_DECLARATION_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace objc {
/// The check for Objective-C global variables and constants naming convention.
/// The declaration should follow the patterns of 'k[A-Z].*' (constants) or
/// 'g[A-Z].*' (variables).
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/google-objc-global-variable-declaration.html
class GlobalVariableDeclarationCheck : public ClangTidyCheck {
public:
GlobalVariableDeclarationCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace objc
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_GLOBAL_VARIABLE_DECLARATION_H

View File

@@ -0,0 +1,105 @@
//===--- 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 "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "../readability/BracesAroundStatementsCheck.h"
#include "../readability/FunctionSizeCheck.h"
#include "../readability/NamespaceCommentCheck.h"
#include "../readability/RedundantSmartptrGetCheck.h"
#include "AvoidCStyleCastsCheck.h"
#include "AvoidThrowingObjCExceptionCheck.h"
#include "DefaultArgumentsCheck.h"
#include "ExplicitConstructorCheck.h"
#include "ExplicitMakePairCheck.h"
#include "GlobalNamesInHeadersCheck.h"
#include "GlobalVariableDeclarationCheck.h"
#include "IntegerTypesCheck.h"
#include "NonConstReferences.h"
#include "OverloadedUnaryAndCheck.h"
#include "StringReferenceMemberCheck.h"
#include "TodoCommentCheck.h"
#include "UnnamedNamespaceInHeaderCheck.h"
#include "UsingNamespaceDirectiveCheck.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
class GoogleModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<build::ExplicitMakePairCheck>(
"google-build-explicit-make-pair");
CheckFactories.registerCheck<build::UnnamedNamespaceInHeaderCheck>(
"google-build-namespaces");
CheckFactories.registerCheck<build::UsingNamespaceDirectiveCheck>(
"google-build-using-namespace");
CheckFactories.registerCheck<DefaultArgumentsCheck>(
"google-default-arguments");
CheckFactories.registerCheck<ExplicitConstructorCheck>(
"google-explicit-constructor");
CheckFactories.registerCheck<readability::GlobalNamesInHeadersCheck>(
"google-global-names-in-headers");
CheckFactories.registerCheck<objc::AvoidThrowingObjCExceptionCheck>(
"google-objc-avoid-throwing-exception");
CheckFactories.registerCheck<objc::GlobalVariableDeclarationCheck>(
"google-objc-global-variable-declaration");
CheckFactories.registerCheck<runtime::IntegerTypesCheck>(
"google-runtime-int");
CheckFactories.registerCheck<runtime::OverloadedUnaryAndCheck>(
"google-runtime-operator");
CheckFactories.registerCheck<runtime::NonConstReferences>(
"google-runtime-references");
CheckFactories.registerCheck<runtime::StringReferenceMemberCheck>(
"google-runtime-member-string-references");
CheckFactories.registerCheck<readability::AvoidCStyleCastsCheck>(
"google-readability-casting");
CheckFactories.registerCheck<readability::TodoCommentCheck>(
"google-readability-todo");
CheckFactories
.registerCheck<clang::tidy::readability::BracesAroundStatementsCheck>(
"google-readability-braces-around-statements");
CheckFactories.registerCheck<clang::tidy::readability::FunctionSizeCheck>(
"google-readability-function-size");
CheckFactories
.registerCheck<clang::tidy::readability::NamespaceCommentCheck>(
"google-readability-namespace-comments");
CheckFactories
.registerCheck<clang::tidy::readability::RedundantSmartptrGetCheck>(
"google-readability-redundant-smartptr-get");
}
ClangTidyOptions getModuleOptions() override {
ClangTidyOptions Options;
auto &Opts = Options.CheckOptions;
Opts["google-readability-braces-around-statements.ShortStatementLines"] =
"1";
Opts["google-readability-function-size.StatementThreshold"] = "800";
Opts["google-readability-namespace-comments.ShortNamespaceLines"] = "10";
Opts["google-readability-namespace-comments.SpacesBeforeComments"] = "2";
return Options;
}
};
// Register the GoogleTidyModule using this statically initialized variable.
static ClangTidyModuleRegistry::Add<GoogleModule> X("google-module",
"Adds Google lint checks.");
} // namespace google
// 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

View File

@@ -0,0 +1,144 @@
//===--- IntegerTypesCheck.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 "IntegerTypesCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Lexer.h"
namespace clang {
using namespace ast_matchers;
static Token getTokenAtLoc(SourceLocation Loc,
const MatchFinder::MatchResult &MatchResult,
IdentifierTable &IdentTable) {
Token Tok;
if (Lexer::getRawToken(Loc, Tok, *MatchResult.SourceManager,
MatchResult.Context->getLangOpts(), false))
return Tok;
if (Tok.is(tok::raw_identifier)) {
IdentifierInfo &Info = IdentTable.get(Tok.getRawIdentifier());
Tok.setIdentifierInfo(&Info);
Tok.setKind(Info.getTokenID());
}
return Tok;
}
namespace tidy {
namespace google {
namespace runtime {
IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
UnsignedTypePrefix(Options.get("UnsignedTypePrefix", "uint")),
SignedTypePrefix(Options.get("SignedTypePrefix", "int")),
TypeSuffix(Options.get("TypeSuffix", "")) {}
void IntegerTypesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "UnsignedTypePrefix", UnsignedTypePrefix);
Options.store(Opts, "SignedTypePrefix", SignedTypePrefix);
Options.store(Opts, "TypeSuffix", TypeSuffix);
}
void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
// Find all TypeLocs. The relevant Style Guide rule only applies to C++.
if (!getLangOpts().CPlusPlus)
return;
Finder->addMatcher(typeLoc(loc(isInteger())).bind("tl"), this);
IdentTable = llvm::make_unique<IdentifierTable>(getLangOpts());
}
void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
SourceLocation Loc = TL.getLocStart();
if (Loc.isInvalid() || Loc.isMacroID())
return;
// Look through qualification.
if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
TL = QualLoc.getUnqualifiedLoc();
auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
if (!BuiltinLoc)
return;
Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
// Ensure the location actually points to one of the builting integral type
// names we're interested in. Otherwise, we might be getting this match from
// implicit code (e.g. an implicit assignment operator of a class containing
// an array of non-POD types).
if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
tok::kw_signed))
return;
bool IsSigned;
unsigned Width;
const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
// Look for uses of short, long, long long and their unsigned versions.
switch (BuiltinLoc.getTypePtr()->getKind()) {
case BuiltinType::Short:
Width = TargetInfo.getShortWidth();
IsSigned = true;
break;
case BuiltinType::Long:
Width = TargetInfo.getLongWidth();
IsSigned = true;
break;
case BuiltinType::LongLong:
Width = TargetInfo.getLongLongWidth();
IsSigned = true;
break;
case BuiltinType::UShort:
Width = TargetInfo.getShortWidth();
IsSigned = false;
break;
case BuiltinType::ULong:
Width = TargetInfo.getLongWidth();
IsSigned = false;
break;
case BuiltinType::ULongLong:
Width = TargetInfo.getLongLongWidth();
IsSigned = false;
break;
default:
return;
}
// We allow "unsigned short port" as that's reasonably common and required by
// the sockets API.
const StringRef Port = "unsigned short port";
const char *Data = Result.SourceManager->getCharacterData(Loc);
if (!std::strncmp(Data, Port.data(), Port.size()) &&
!isIdentifierBody(Data[Port.size()]))
return;
std::string Replacement =
((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
TypeSuffix)
.str();
// We don't add a fix-it as changing the type can easily break code,
// e.g. when a function requires a 'long' argument on all platforms.
// QualTypes are printed with implicit quotes.
diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType()
<< Replacement;
}
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,49 @@
//===--- IntegerTypesCheck.h - clang-tidy -----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_INTEGERTYPESCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_INTEGERTYPESCHECK_H
#include "../ClangTidy.h"
#include <memory>
namespace clang {
class IdentifierTable;
namespace tidy {
namespace google {
namespace runtime {
/// Finds uses of `short`, `long` and `long long` and suggest replacing them
/// with `u?intXX(_t)?`.
///
/// Correspondig cpplint.py check: 'runtime/int'.
class IntegerTypesCheck : public ClangTidyCheck {
public:
IntegerTypesCheck(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Options) override;
private:
const std::string UnsignedTypePrefix;
const std::string SignedTypePrefix;
const std::string TypeSuffix;
std::unique_ptr<IdentifierTable> IdentTable;
};
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_INTEGERTYPESCHECK_H

View File

@@ -0,0 +1,149 @@
//===--- NonConstReferences.cpp - clang-tidy --------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "NonConstReferences.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/DeclBase.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace runtime {
NonConstReferences::NonConstReferences(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
WhiteListTypes(
utils::options::parseStringList(Options.get("WhiteListTypes", ""))) {}
void NonConstReferences::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "WhiteListTypes",
utils::options::serializeStringList(WhiteListTypes));
}
void NonConstReferences::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
Finder->addMatcher(
parmVarDecl(
unless(isInstantiated()),
hasType(references(
qualType(unless(isConstQualified())).bind("referenced_type"))),
unless(hasType(rValueReferenceType())))
.bind("param"),
this);
}
void NonConstReferences::check(const MatchFinder::MatchResult &Result) {
const auto *Parameter = Result.Nodes.getNodeAs<ParmVarDecl>("param");
const auto *Function =
dyn_cast_or_null<FunctionDecl>(Parameter->getParentFunctionOrMethod());
if (Function == nullptr || Function->isImplicit())
return;
if (!Function->isCanonicalDecl())
return;
if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
// Don't warn on implementations of an interface using references.
if (Method->begin_overridden_methods() != Method->end_overridden_methods())
return;
// Don't warn on lambdas, as they frequently have to conform to the
// interface defined elsewhere.
if (Method->getParent()->isLambda())
return;
}
auto ReferencedType = *Result.Nodes.getNodeAs<QualType>("referenced_type");
if (std::find_if(WhiteListTypes.begin(), WhiteListTypes.end(),
[&](llvm::StringRef WhiteListType) {
return ReferencedType.getCanonicalType().getAsString(
Result.Context->getPrintingPolicy()) ==
WhiteListType;
}) != WhiteListTypes.end())
return;
// Don't warn on function references, they shouldn't be constant.
if (ReferencedType->isFunctionProtoType())
return;
// Don't warn on dependent types in templates.
if (ReferencedType->isDependentType())
return;
if (Function->isOverloadedOperator()) {
switch (Function->getOverloadedOperator()) {
case clang::OO_LessLess:
case clang::OO_PlusPlus:
case clang::OO_MinusMinus:
case clang::OO_PlusEqual:
case clang::OO_MinusEqual:
case clang::OO_StarEqual:
case clang::OO_SlashEqual:
case clang::OO_PercentEqual:
case clang::OO_LessLessEqual:
case clang::OO_GreaterGreaterEqual:
case clang::OO_PipeEqual:
case clang::OO_CaretEqual:
case clang::OO_AmpEqual:
// Don't warn on the first parameter of operator<<(Stream&, ...),
// operator++, operator-- and operation+assignment operators.
if (Function->getParamDecl(0) == Parameter)
return;
break;
case clang::OO_GreaterGreater: {
auto isNonConstRef = [](clang::QualType T) {
return T->isReferenceType() &&
!T.getNonReferenceType().isConstQualified();
};
// Don't warn on parameters of stream extractors:
// Stream& operator>>(Stream&, Value&);
// Both parameters should be non-const references by convention.
if (isNonConstRef(Function->getParamDecl(0)->getType()) &&
(Function->getNumParams() < 2 || // E.g. member operator>>.
isNonConstRef(Function->getParamDecl(1)->getType())) &&
isNonConstRef(Function->getReturnType()))
return;
break;
}
default:
break;
}
}
// Some functions use references to comply with established standards.
if (Function->getDeclName().isIdentifier() && Function->getName() == "swap")
return;
// iostream parameters are typically passed by non-const reference.
if (StringRef(ReferencedType.getAsString()).endswith("stream"))
return;
if (Parameter->getName().empty()) {
diag(Parameter->getLocation(), "non-const reference parameter at index %0, "
"make it const or use a pointer")
<< Parameter->getFunctionScopeIndex();
} else {
diag(Parameter->getLocation(),
"non-const reference parameter %0, make it const or use a pointer")
<< Parameter;
}
}
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,39 @@
//===--- NonConstReferences.h - clang-tidy ----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_NON_CONST_REFERENCES_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_NON_CONST_REFERENCES_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace google {
namespace runtime {
/// \brief Checks the usage of non-constant references in function parameters.
///
/// https://google.github.io/styleguide/cppguide.html#Reference_Arguments
class NonConstReferences : public ClangTidyCheck {
public:
NonConstReferences(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
private:
const std::vector<std::string> WhiteListTypes;
};
} // namespace runtime
} // namespace google
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_NON_CONST_REFERENCES_H

Some files were not shown because too many files have changed in this diff Show More