Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {

void RedundantControlFlowCheck::checkRedundantReturn(
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
if (const auto *Return = dyn_cast<ReturnStmt>(*last))
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
if (const auto *Return = dyn_cast<ReturnStmt>(*Last))
issueDiagnostic(Result, Block, Return->getSourceRange(),
RedundantReturnDiag);
}

void RedundantControlFlowCheck::checkRedundantContinue(
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
if (const auto *Continue = dyn_cast<ContinueStmt>(*last))
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
if (const auto *Continue = dyn_cast<ContinueStmt>(*Last))
issueDiagnostic(Result, Block, Continue->getSourceRange(),
RedundantContinueDiag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
bool MultiVar = false;
if (const auto *VD = dyn_cast<VarDecl>(D)) {
// Is this a multivariable declaration?
for (const auto Other : VD->getDeclContext()->decls()) {
for (const auto *Other : VD->getDeclContext()->decls()) {
if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
MultiVar = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ class RedundantPreprocessorCallbacks : public PPCallbacks {
StringRef Condition =
Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),
PP.getSourceManager(), PP.getLangOpts());
CheckMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true);
checkMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true);
}

void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
std::string MacroName = PP.getSpelling(MacroNameTok);
CheckMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true);
CheckMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true);
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
false);
}

void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
std::string MacroName = PP.getSpelling(MacroNameTok);
CheckMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
true);
CheckMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
false);
}

Expand All @@ -70,7 +70,7 @@ class RedundantPreprocessorCallbacks : public PPCallbacks {
}

private:
void CheckMacroRedundancy(SourceLocation Loc, StringRef MacroName,
void checkMacroRedundancy(SourceLocation Loc, StringRef MacroName,
SmallVector<PreprocessorEntry, 4> &Stack,
DirectiveKind WarningKind, DirectiveKind NoteKind,
bool Store) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}

void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
const auto hasStringTypeName = hasAnyNameStdString(StringNames);
const auto hasStringCtorName =
const auto HasStringTypeName = hasAnyNameStdString(StringNames);
const auto HasStringCtorName =
hasAnyNameStdString(removeNamespaces(StringNames));

// Match string constructor.
const auto StringConstructorExpr = expr(
anyOf(cxxConstructExpr(argumentCountIs(1),
hasDeclaration(cxxMethodDecl(hasStringCtorName))),
hasDeclaration(cxxMethodDecl(HasStringCtorName))),
// If present, the second argument is the alloc object which must
// not be present explicitly.
cxxConstructExpr(argumentCountIs(2),
hasDeclaration(cxxMethodDecl(hasStringCtorName)),
hasDeclaration(cxxMethodDecl(HasStringCtorName)),
hasArgument(1, cxxDefaultArgExpr()))));

// Match a string constructor expression with an empty string literal.
Expand All @@ -96,7 +96,7 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));

const auto StringType = hasType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(cxxRecordDecl(hasStringTypeName)))));
recordType(hasDeclaration(cxxRecordDecl(HasStringTypeName)))));
const auto EmptyStringInit = traverse(
TK_AsIs, expr(ignoringImplicit(anyOf(
EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ StringRef negatedOperator(const CXXOperatorCallExpr *OpCall) {
return StringRef();
}

std::string asBool(StringRef text, bool NeedsStaticCast) {
std::string asBool(StringRef Text, bool NeedsStaticCast) {
if (NeedsStaticCast)
return ("static_cast<bool>(" + text + ")").str();
return ("static_cast<bool>(" + Text + ")").str();

return std::string(text);
return std::string(Text);
}

bool needsNullPtrComparison(const Expr *E) {
Expand Down Expand Up @@ -398,8 +398,8 @@ void SimplifyBooleanExprCheck::reportBinOp(

bool BoolValue = Bool->getValue();

auto replaceWithExpression = [this, &Result, LHS, RHS, Bool](
const Expr *ReplaceWith, bool Negated) {
auto ReplaceWithExpression = [this, &Result, LHS, RHS,
Bool](const Expr *ReplaceWith, bool Negated) {
std::string Replacement =
replacementExpression(Result, Negated, ReplaceWith);
SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
Expand All @@ -411,28 +411,28 @@ void SimplifyBooleanExprCheck::reportBinOp(
case BO_LAnd:
if (BoolValue) {
// expr && true -> expr
replaceWithExpression(Other, /*Negated=*/false);
ReplaceWithExpression(Other, /*Negated=*/false);
} else {
// expr && false -> false
replaceWithExpression(Bool, /*Negated=*/false);
ReplaceWithExpression(Bool, /*Negated=*/false);
}
break;
case BO_LOr:
if (BoolValue) {
// expr || true -> true
replaceWithExpression(Bool, /*Negated=*/false);
ReplaceWithExpression(Bool, /*Negated=*/false);
} else {
// expr || false -> expr
replaceWithExpression(Other, /*Negated=*/false);
ReplaceWithExpression(Other, /*Negated=*/false);
}
break;
case BO_EQ:
// expr == true -> expr, expr == false -> !expr
replaceWithExpression(Other, /*Negated=*/!BoolValue);
ReplaceWithExpression(Other, /*Negated=*/!BoolValue);
break;
case BO_NE:
// expr != true -> !expr, expr != false -> expr
replaceWithExpression(Other, /*Negated=*/BoolValue);
ReplaceWithExpression(Other, /*Negated=*/BoolValue);
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace clang {
namespace tidy {
namespace readability {

static const char kDefaultTypes[] =
static const char KDefaultTypes[] =
"::std::basic_string;::std::basic_string_view;::std::vector;::std::array";

SimplifySubscriptExprCheck::SimplifySubscriptExprCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), Types(utils::options::parseStringList(
Options.get("Types", kDefaultTypes))) {
Options.get("Types", KDefaultTypes))) {
}

void SimplifySubscriptExprCheck::registerMatchers(MatchFinder *Finder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct NewSuffix {
llvm::Optional<FixItHint> FixIt;
};

llvm::Optional<SourceLocation> GetMacroAwareLocation(SourceLocation Loc,
llvm::Optional<SourceLocation> getMacroAwareLocation(SourceLocation Loc,
const SourceManager &SM) {
// Do nothing if the provided location is invalid.
if (Loc.isInvalid())
Expand All @@ -74,11 +74,11 @@ llvm::Optional<SourceLocation> GetMacroAwareLocation(SourceLocation Loc,
return SpellingLoc;
}

llvm::Optional<SourceRange> GetMacroAwareSourceRange(SourceRange Loc,
llvm::Optional<SourceRange> getMacroAwareSourceRange(SourceRange Loc,
const SourceManager &SM) {
llvm::Optional<SourceLocation> Begin =
GetMacroAwareLocation(Loc.getBegin(), SM);
llvm::Optional<SourceLocation> End = GetMacroAwareLocation(Loc.getEnd(), SM);
getMacroAwareLocation(Loc.getBegin(), SM);
llvm::Optional<SourceLocation> End = getMacroAwareLocation(Loc.getEnd(), SM);
if (!Begin || !End)
return llvm::None;
return SourceRange(*Begin, *End);
Expand Down Expand Up @@ -120,7 +120,7 @@ shouldReplaceLiteralSuffix(const Expr &Literal,

// The literal may have macro expansion, we need the final expanded src range.
llvm::Optional<SourceRange> Range =
GetMacroAwareSourceRange(ReplacementDsc.LiteralLocation, SM);
getMacroAwareSourceRange(ReplacementDsc.LiteralLocation, SM);
if (!Range)
return llvm::None;

Expand Down
18 changes: 9 additions & 9 deletions clang-tools-extra/clang-tidy/readability/UseAnyOfAllOfCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@ namespace tidy {
namespace readability {

void UseAnyOfAllOfCheck::registerMatchers(MatchFinder *Finder) {
auto returns = [](bool V) {
auto Returns = [](bool V) {
return returnStmt(hasReturnValue(cxxBoolLiteral(equals(V))));
};

auto returnsButNotTrue =
auto ReturnsButNotTrue =
returnStmt(hasReturnValue(unless(cxxBoolLiteral(equals(true)))));
auto returnsButNotFalse =
auto ReturnsButNotFalse =
returnStmt(hasReturnValue(unless(cxxBoolLiteral(equals(false)))));

Finder->addMatcher(
cxxForRangeStmt(
nextStmt(returns(false).bind("final_return")),
hasBody(allOf(hasDescendant(returns(true)),
nextStmt(Returns(false).bind("final_return")),
hasBody(allOf(hasDescendant(Returns(true)),
unless(anyOf(hasDescendant(breakStmt()),
hasDescendant(gotoStmt()),
hasDescendant(returnsButNotTrue))))))
hasDescendant(ReturnsButNotTrue))))))
.bind("any_of_loop"),
this);

Finder->addMatcher(
cxxForRangeStmt(
nextStmt(returns(true).bind("final_return")),
hasBody(allOf(hasDescendant(returns(false)),
nextStmt(Returns(true).bind("final_return")),
hasBody(allOf(hasDescendant(Returns(false)),
unless(anyOf(hasDescendant(breakStmt()),
hasDescendant(gotoStmt()),
hasDescendant(returnsButNotFalse))))))
hasDescendant(ReturnsButNotFalse))))))
.bind("all_of_loop"),
this);
}
Expand Down
10 changes: 4 additions & 6 deletions clang-tools-extra/clang-tidy/utils/ASTUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ const FunctionDecl *getSurroundingFunction(ASTContext &Context,
}

bool IsBinaryOrTernary(const Expr *E) {
const Expr *E_base = E->IgnoreImpCasts();
if (clang::isa<clang::BinaryOperator>(E_base) ||
clang::isa<clang::ConditionalOperator>(E_base)) {
const Expr *EBase = E->IgnoreImpCasts();
if (isa<BinaryOperator>(EBase) || isa<ConditionalOperator>(EBase)) {
return true;
}

if (const auto *Operator =
clang::dyn_cast<clang::CXXOperatorCallExpr>(E_base)) {
if (const auto *Operator = dyn_cast<CXXOperatorCallExpr>(EBase)) {
return Operator->isInfixBinaryOp();
}

Expand All @@ -56,7 +54,7 @@ bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM,
}
// If it's a binary OR operation.
if (const auto *BO = dyn_cast<BinaryOperator>(Flags))
if (BO->getOpcode() == clang::BinaryOperatorKind::BO_Or)
if (BO->getOpcode() == BinaryOperatorKind::BO_Or)
return exprHasBitFlagWithSpelling(BO->getLHS()->IgnoreParenCasts(), SM,
LangOpts, FlagName) ||
exprHasBitFlagWithSpelling(BO->getRHS()->IgnoreParenCasts(), SM,
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
} else if (const auto *Try = dyn_cast<CXXTryStmt>(St)) {
ExceptionInfo Uncaught =
throwsException(Try->getTryBlock(), Caught, CallStack);
for (unsigned i = 0; i < Try->getNumHandlers(); ++i) {
const CXXCatchStmt *Catch = Try->getHandler(i);
for (unsigned I = 0; I < Try->getNumHandlers(); ++I) {
const CXXCatchStmt *Catch = Try->getHandler(I);

// Everything is catched through 'catch(...)'.
if (!Catch->getExceptionDecl()) {
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,

return false;
}
}
} // namespace

ExprSequence::ExprSequence(const CFG *TheCFG, const Stmt *Root,
ASTContext *TheContext)
Expand Down
37 changes: 19 additions & 18 deletions clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace utils {

namespace {

StringRef RemoveFirstSuffix(StringRef Str, ArrayRef<const char *> Suffixes) {
StringRef removeFirstSuffix(StringRef Str, ArrayRef<const char *> Suffixes) {
for (StringRef Suffix : Suffixes) {
if (Str.endswith(Suffix)) {
return Str.substr(0, Str.size() - Suffix.size());
Expand All @@ -26,20 +26,20 @@ StringRef RemoveFirstSuffix(StringRef Str, ArrayRef<const char *> Suffixes) {
return Str;
}

StringRef MakeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) {
StringRef makeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) {
// The list of suffixes to remove from source file names to get the
// "canonical" file names.
// E.g. tools/sort_includes.cc and tools/sort_includes_test.cc
// would both canonicalize to tools/sort_includes and tools/sort_includes.h
// (once canonicalized) will match as being the main include file associated
// with the source files.
if (Style == IncludeSorter::IS_LLVM) {
return RemoveFirstSuffix(
RemoveFirstSuffix(Str, {".cc", ".cpp", ".c", ".h", ".hpp"}), {"Test"});
return removeFirstSuffix(
removeFirstSuffix(Str, {".cc", ".cpp", ".c", ".h", ".hpp"}), {"Test"});
}
if (Style == IncludeSorter::IS_Google_ObjC) {
StringRef Canonical =
RemoveFirstSuffix(RemoveFirstSuffix(Str, {".cc", ".cpp", ".c", ".h",
removeFirstSuffix(removeFirstSuffix(Str, {".cc", ".cpp", ".c", ".h",
".hpp", ".mm", ".m"}),
{"_unittest", "_regtest", "_test", "Test"});

Expand All @@ -52,19 +52,19 @@ StringRef MakeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) {
return Canonical.substr(
0, Canonical.find_first_of('+', StartIndex));
}
return RemoveFirstSuffix(
RemoveFirstSuffix(Str, {".cc", ".cpp", ".c", ".h", ".hpp"}),
return removeFirstSuffix(
removeFirstSuffix(Str, {".cc", ".cpp", ".c", ".h", ".hpp"}),
{"_unittest", "_regtest", "_test"});
}

// Scan to the end of the line and return the offset of the next line.
size_t FindNextLine(const char *Text) {
size_t findNextLine(const char *Text) {
size_t EOLIndex = std::strcspn(Text, "\n");
return Text[EOLIndex] == '\0' ? EOLIndex : EOLIndex + 1;
}

IncludeSorter::IncludeKinds
DetermineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
determineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
bool IsAngled, IncludeSorter::IncludeStyle Style) {
// Compute the two "canonical" forms of the include's filename sans extension.
// The first form is the include's filename without ".h" or "-inl.h" at the
Expand All @@ -76,7 +76,7 @@ DetermineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
return IncludeFile.endswith(".h") ? IncludeSorter::IK_CSystemInclude
: IncludeSorter::IK_CXXSystemInclude;
}
StringRef CanonicalInclude = MakeCanonicalName(IncludeFile, Style);
StringRef CanonicalInclude = makeCanonicalName(IncludeFile, Style);
if (CanonicalFile.endswith(CanonicalInclude)
|| CanonicalInclude.endswith(CanonicalFile)) {
return IncludeSorter::IK_MainTUInclude;
Expand Down Expand Up @@ -127,12 +127,12 @@ IncludeSorter::IncludeSorter(const SourceManager *SourceMgr,
const FileID FileID, StringRef FileName,
IncludeStyle Style)
: SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
CanonicalFile(MakeCanonicalName(FileName, Style)) {}
CanonicalFile(makeCanonicalName(FileName, Style)) {}

void IncludeSorter::AddInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation,
SourceLocation EndLocation) {
int Offset = FindNextLine(SourceMgr->getCharacterData(EndLocation));
int Offset = findNextLine(SourceMgr->getCharacterData(EndLocation));

// Record the relevant location information for this inclusion directive.
IncludeLocations[FileName].push_back(
Expand All @@ -145,7 +145,7 @@ void IncludeSorter::AddInclude(StringRef FileName, bool IsAngled,

// Add the included file's name to the appropriate bucket.
IncludeKinds Kind =
DetermineIncludeKind(CanonicalFile, FileName, IsAngled, Style);
determineIncludeKind(CanonicalFile, FileName, IsAngled, Style);
if (Kind != IK_InvalidInclude)
IncludeBucket[Kind].push_back(FileName.str());
}
Expand All @@ -171,14 +171,15 @@ Optional<FixItHint> IncludeSorter::CreateIncludeInsertion(StringRef FileName,
}

auto IncludeKind =
DetermineIncludeKind(CanonicalFile, FileName, IsAngled, Style);
determineIncludeKind(CanonicalFile, FileName, IsAngled, Style);

if (!IncludeBucket[IncludeKind].empty()) {
for (const std::string &IncludeEntry : IncludeBucket[IncludeKind]) {
if (compareHeaders(FileName, IncludeEntry, Style) < 0) {
const auto &Location = IncludeLocations[IncludeEntry][0];
return FixItHint::CreateInsertion(Location.getBegin(), IncludeStmt);
} else if (FileName == IncludeEntry) {
}
if (FileName == IncludeEntry) {
return llvm::None;
}
}
Expand All @@ -195,9 +196,9 @@ Optional<FixItHint> IncludeSorter::CreateIncludeInsertion(StringRef FileName,
// include bucket in the file. In that case, we'll want to sort the include
// before that bucket.
IncludeKinds NonEmptyKind = IK_InvalidInclude;
for (int i = IK_InvalidInclude - 1; i >= 0; --i) {
if (!IncludeBucket[i].empty()) {
NonEmptyKind = static_cast<IncludeKinds>(i);
for (int I = IK_InvalidInclude - 1; I >= 0; --I) {
if (!IncludeBucket[I].empty()) {
NonEmptyKind = static_cast<IncludeKinds>(I);
if (NonEmptyKind < IncludeKind)
break;
}
Expand Down