2016-04-01 02:12:23 +08:00
|
|
|
//===--- SuspiciousMissingCommaCheck.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 "SuspiciousMissingCommaCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2018-03-01 07:30:29 +08:00
|
|
|
namespace bugprone {
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
bool isConcatenatedLiteralsOnPurpose(ASTContext *Ctx,
|
|
|
|
const StringLiteral *Lit) {
|
2016-04-04 23:46:38 +08:00
|
|
|
// String literals surrounded by parentheses are assumed to be on purpose.
|
|
|
|
// i.e.: const char* Array[] = { ("a" "b" "c"), "d", [...] };
|
|
|
|
auto Parents = Ctx->getParents(*Lit);
|
|
|
|
if (Parents.size() == 1 && Parents[0].get<ParenExpr>() != nullptr)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Appropriately indented string literals are assumed to be on purpose.
|
|
|
|
// The following frequent indentation is accepted:
|
|
|
|
// const char* Array[] = {
|
|
|
|
// "first literal"
|
|
|
|
// "indented literal"
|
|
|
|
// "indented literal",
|
|
|
|
// "second literal",
|
|
|
|
// [...]
|
|
|
|
// };
|
2016-11-08 15:50:19 +08:00
|
|
|
const SourceManager &SM = Ctx->getSourceManager();
|
2016-04-04 23:46:38 +08:00
|
|
|
bool IndentedCorrectly = true;
|
|
|
|
SourceLocation FirstToken = Lit->getStrTokenLoc(0);
|
|
|
|
FileID BaseFID = SM.getFileID(FirstToken);
|
|
|
|
unsigned int BaseIndent = SM.getSpellingColumnNumber(FirstToken);
|
|
|
|
unsigned int BaseLine = SM.getSpellingLineNumber(FirstToken);
|
2016-11-08 15:50:19 +08:00
|
|
|
for (unsigned int TokNum = 1; TokNum < Lit->getNumConcatenated(); ++TokNum) {
|
2016-04-04 23:46:38 +08:00
|
|
|
SourceLocation Token = Lit->getStrTokenLoc(TokNum);
|
|
|
|
FileID FID = SM.getFileID(Token);
|
|
|
|
unsigned int Indent = SM.getSpellingColumnNumber(Token);
|
|
|
|
unsigned int Line = SM.getSpellingLineNumber(Token);
|
|
|
|
if (FID != BaseFID || Line != BaseLine + TokNum || Indent <= BaseIndent) {
|
|
|
|
IndentedCorrectly = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IndentedCorrectly)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// There is no pattern recognized by the checker, assume it's not on purpose.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
AST_MATCHER_P(StringLiteral, isConcatenatedLiteral, unsigned,
|
|
|
|
MaxConcatenatedTokens) {
|
2016-04-04 23:46:38 +08:00
|
|
|
return Node.getNumConcatenated() > 1 &&
|
|
|
|
Node.getNumConcatenated() < MaxConcatenatedTokens &&
|
|
|
|
!isConcatenatedLiteralsOnPurpose(&Finder->getASTContext(), &Node);
|
2016-04-01 02:12:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
} // namespace
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
SuspiciousMissingCommaCheck::SuspiciousMissingCommaCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
SizeThreshold(Options.get("SizeThreshold", 5U)),
|
2016-04-04 23:46:38 +08:00
|
|
|
RatioThreshold(std::stod(Options.get("RatioThreshold", ".2"))),
|
|
|
|
MaxConcatenatedTokens(Options.get("MaxConcatenatedTokens", 5U)) {}
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
void SuspiciousMissingCommaCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "SizeThreshold", SizeThreshold);
|
|
|
|
Options.store(Opts, "RatioThreshold", std::to_string(RatioThreshold));
|
2016-04-04 23:46:38 +08:00
|
|
|
Options.store(Opts, "MaxConcatenatedTokens", MaxConcatenatedTokens);
|
2016-04-01 02:12:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuspiciousMissingCommaCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
const auto ConcatenatedStringLiteral =
|
2016-04-04 23:46:38 +08:00
|
|
|
stringLiteral(isConcatenatedLiteral(MaxConcatenatedTokens)).bind("str");
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
const auto StringsInitializerList =
|
|
|
|
initListExpr(hasType(constantArrayType()),
|
2016-05-31 23:26:56 +08:00
|
|
|
has(ignoringParenImpCasts(expr(ConcatenatedStringLiteral))));
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(StringsInitializerList.bind("list"), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuspiciousMissingCommaCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *InitializerList = Result.Nodes.getNodeAs<InitListExpr>("list");
|
2016-04-04 23:46:38 +08:00
|
|
|
const auto *ConcatenatedLiteral =
|
|
|
|
Result.Nodes.getNodeAs<StringLiteral>("str");
|
2016-04-01 02:12:23 +08:00
|
|
|
assert(InitializerList && ConcatenatedLiteral);
|
2016-08-01 20:06:18 +08:00
|
|
|
|
2016-04-01 02:12:23 +08:00
|
|
|
// Skip small arrays as they often generate false-positive.
|
|
|
|
unsigned int Size = InitializerList->getNumInits();
|
2016-11-08 15:50:19 +08:00
|
|
|
if (Size < SizeThreshold)
|
|
|
|
return;
|
2016-04-01 02:12:23 +08:00
|
|
|
|
|
|
|
// Count the number of occurence of concatenated string literal.
|
|
|
|
unsigned int Count = 0;
|
|
|
|
for (unsigned int i = 0; i < Size; ++i) {
|
|
|
|
const Expr *Child = InitializerList->getInit(i)->IgnoreImpCasts();
|
|
|
|
if (const auto *Literal = dyn_cast<StringLiteral>(Child)) {
|
2016-11-08 15:50:19 +08:00
|
|
|
if (Literal->getNumConcatenated() > 1)
|
|
|
|
++Count;
|
2016-04-01 02:12:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn only when concatenation is not common in this initializer list.
|
|
|
|
// The current threshold is set to less than 1/5 of the string literals.
|
2016-11-08 15:50:19 +08:00
|
|
|
if (double(Count) / Size > RatioThreshold)
|
|
|
|
return;
|
2016-04-01 02:12:23 +08:00
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(ConcatenatedLiteral->getBeginLoc(),
|
2016-04-01 02:12:23 +08:00
|
|
|
"suspicious string literal, probably missing a comma");
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:30:29 +08:00
|
|
|
} // namespace bugprone
|
2016-11-08 15:50:19 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|