2015-10-28 09:36:20 +08:00
|
|
|
//===- RedundantVoidArgCheck.cpp - clang-tidy -----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-10-28 09:36:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RedundantVoidArgCheck.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
2015-10-28 09:36:20 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Determine if the given QualType is a nullary function or pointer to same.
|
|
|
|
bool protoTypeHasNoParms(QualType QT) {
|
|
|
|
if (auto PT = QT->getAs<PointerType>()) {
|
|
|
|
QT = PT->getPointeeType();
|
|
|
|
}
|
|
|
|
if (auto *MPT = QT->getAs<MemberPointerType>()) {
|
|
|
|
QT = MPT->getPointeeType();
|
|
|
|
}
|
|
|
|
if (auto FP = QT->getAs<FunctionProtoType>()) {
|
|
|
|
return FP->getNumParams() == 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char FunctionId[] = "function";
|
|
|
|
const char TypedefId[] = "typedef";
|
|
|
|
const char FieldId[] = "field";
|
|
|
|
const char VarId[] = "var";
|
|
|
|
const char NamedCastId[] = "named-cast";
|
|
|
|
const char CStyleCastId[] = "c-style-cast";
|
|
|
|
const char ExplicitCastId[] = "explicit-cast";
|
|
|
|
const char LambdaId[] = "lambda";
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::registerMatchers(MatchFinder *Finder) {
|
2016-09-24 10:13:45 +08:00
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2016-02-16 17:49:05 +08:00
|
|
|
Finder->addMatcher(functionDecl(parameterCountIs(0), unless(isImplicit()),
|
2018-09-17 20:29:29 +08:00
|
|
|
unless(isInstantiated()), unless(isExternC()))
|
2015-11-02 19:39:17 +08:00
|
|
|
.bind(FunctionId),
|
2015-10-28 09:36:20 +08:00
|
|
|
this);
|
2016-04-15 03:28:13 +08:00
|
|
|
Finder->addMatcher(typedefNameDecl().bind(TypedefId), this);
|
2015-10-28 09:36:20 +08:00
|
|
|
auto ParenFunctionType = parenType(innerType(functionType()));
|
|
|
|
auto PointerToFunctionType = pointee(ParenFunctionType);
|
|
|
|
auto FunctionOrMemberPointer =
|
|
|
|
anyOf(hasType(pointerType(PointerToFunctionType)),
|
|
|
|
hasType(memberPointerType(PointerToFunctionType)));
|
2016-02-16 17:49:05 +08:00
|
|
|
Finder->addMatcher(fieldDecl(FunctionOrMemberPointer).bind(FieldId), this);
|
|
|
|
Finder->addMatcher(varDecl(FunctionOrMemberPointer).bind(VarId), this);
|
2015-10-28 09:36:20 +08:00
|
|
|
auto CastDestinationIsFunction =
|
|
|
|
hasDestinationType(pointsTo(ParenFunctionType));
|
|
|
|
Finder->addMatcher(
|
2016-02-16 17:49:05 +08:00
|
|
|
cStyleCastExpr(CastDestinationIsFunction).bind(CStyleCastId), this);
|
2015-10-28 09:36:20 +08:00
|
|
|
Finder->addMatcher(
|
2016-02-16 17:49:05 +08:00
|
|
|
cxxStaticCastExpr(CastDestinationIsFunction).bind(NamedCastId), this);
|
2015-10-28 09:36:20 +08:00
|
|
|
Finder->addMatcher(
|
2016-02-16 17:49:05 +08:00
|
|
|
cxxReinterpretCastExpr(CastDestinationIsFunction).bind(NamedCastId),
|
2015-10-28 09:36:20 +08:00
|
|
|
this);
|
2015-11-02 19:39:17 +08:00
|
|
|
Finder->addMatcher(
|
2016-02-16 17:49:05 +08:00
|
|
|
cxxConstCastExpr(CastDestinationIsFunction).bind(NamedCastId), this);
|
|
|
|
Finder->addMatcher(lambdaExpr().bind(LambdaId), this);
|
2015-10-28 09:36:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const BoundNodes &Nodes = Result.Nodes;
|
|
|
|
if (const auto *Function = Nodes.getNodeAs<FunctionDecl>(FunctionId)) {
|
|
|
|
processFunctionDecl(Result, Function);
|
2016-04-15 03:28:13 +08:00
|
|
|
} else if (const auto *TypedefName =
|
|
|
|
Nodes.getNodeAs<TypedefNameDecl>(TypedefId)) {
|
|
|
|
processTypedefNameDecl(Result, TypedefName);
|
2015-10-28 09:36:20 +08:00
|
|
|
} else if (const auto *Member = Nodes.getNodeAs<FieldDecl>(FieldId)) {
|
|
|
|
processFieldDecl(Result, Member);
|
|
|
|
} else if (const auto *Var = Nodes.getNodeAs<VarDecl>(VarId)) {
|
|
|
|
processVarDecl(Result, Var);
|
|
|
|
} else if (const auto *NamedCast =
|
|
|
|
Nodes.getNodeAs<CXXNamedCastExpr>(NamedCastId)) {
|
|
|
|
processNamedCastExpr(Result, NamedCast);
|
|
|
|
} else if (const auto *CStyleCast =
|
|
|
|
Nodes.getNodeAs<CStyleCastExpr>(CStyleCastId)) {
|
|
|
|
processExplicitCastExpr(Result, CStyleCast);
|
|
|
|
} else if (const auto *ExplicitCast =
|
|
|
|
Nodes.getNodeAs<ExplicitCastExpr>(ExplicitCastId)) {
|
|
|
|
processExplicitCastExpr(Result, ExplicitCast);
|
|
|
|
} else if (const auto *Lambda = Nodes.getNodeAs<LambdaExpr>(LambdaId)) {
|
|
|
|
processLambdaExpr(Result, Lambda);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processFunctionDecl(
|
|
|
|
const MatchFinder::MatchResult &Result, const FunctionDecl *Function) {
|
|
|
|
if (Function->isThisDeclarationADefinition()) {
|
2016-03-22 21:37:44 +08:00
|
|
|
const Stmt *Body = Function->getBody();
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Start = Function->getBeginLoc();
|
2016-04-15 03:28:13 +08:00
|
|
|
SourceLocation End =
|
2018-08-10 06:43:02 +08:00
|
|
|
Body ? Body->getBeginLoc().getLocWithOffset(-1) : Function->getEndLoc();
|
2015-11-03 00:18:23 +08:00
|
|
|
removeVoidArgumentTokens(Result, SourceRange(Start, End),
|
2015-10-28 09:36:20 +08:00
|
|
|
"function definition");
|
|
|
|
} else {
|
|
|
|
removeVoidArgumentTokens(Result, Function->getSourceRange(),
|
|
|
|
"function declaration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::removeVoidArgumentTokens(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result, SourceRange Range,
|
|
|
|
StringRef GrammarLocation) {
|
2016-09-24 10:13:45 +08:00
|
|
|
CharSourceRange CharRange =
|
|
|
|
Lexer::makeFileCharRange(CharSourceRange::getTokenRange(Range),
|
|
|
|
*Result.SourceManager, getLangOpts());
|
2015-11-02 19:39:17 +08:00
|
|
|
|
2016-09-24 10:13:45 +08:00
|
|
|
std::string DeclText =
|
|
|
|
Lexer::getSourceText(CharRange, *Result.SourceManager, getLangOpts())
|
|
|
|
.str();
|
|
|
|
Lexer PrototypeLexer(CharRange.getBegin(), getLangOpts(), DeclText.data(),
|
|
|
|
DeclText.data(), DeclText.data() + DeclText.size());
|
2015-10-28 09:36:20 +08:00
|
|
|
enum TokenState {
|
|
|
|
NothingYet,
|
|
|
|
SawLeftParen,
|
|
|
|
SawVoid,
|
|
|
|
};
|
|
|
|
TokenState State = NothingYet;
|
|
|
|
Token VoidToken;
|
|
|
|
Token ProtoToken;
|
|
|
|
std::string Diagnostic =
|
|
|
|
("redundant void argument list in " + GrammarLocation).str();
|
|
|
|
|
|
|
|
while (!PrototypeLexer.LexFromRawLexer(ProtoToken)) {
|
|
|
|
switch (State) {
|
|
|
|
case NothingYet:
|
|
|
|
if (ProtoToken.is(tok::TokenKind::l_paren)) {
|
|
|
|
State = SawLeftParen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SawLeftParen:
|
|
|
|
if (ProtoToken.is(tok::TokenKind::raw_identifier) &&
|
|
|
|
ProtoToken.getRawIdentifier() == "void") {
|
|
|
|
State = SawVoid;
|
|
|
|
VoidToken = ProtoToken;
|
2018-08-10 21:59:33 +08:00
|
|
|
} else if (ProtoToken.is(tok::TokenKind::l_paren)) {
|
|
|
|
State = SawLeftParen;
|
2015-10-28 09:36:20 +08:00
|
|
|
} else {
|
|
|
|
State = NothingYet;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SawVoid:
|
|
|
|
State = NothingYet;
|
|
|
|
if (ProtoToken.is(tok::TokenKind::r_paren)) {
|
|
|
|
removeVoidToken(VoidToken, Diagnostic);
|
|
|
|
} else if (ProtoToken.is(tok::TokenKind::l_paren)) {
|
|
|
|
State = SawLeftParen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (State == SawVoid && ProtoToken.is(tok::TokenKind::r_paren)) {
|
|
|
|
removeVoidToken(VoidToken, Diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::removeVoidToken(Token VoidToken,
|
|
|
|
StringRef Diagnostic) {
|
|
|
|
SourceLocation VoidLoc(VoidToken.getLocation());
|
|
|
|
auto VoidRange =
|
|
|
|
CharSourceRange::getTokenRange(VoidLoc, VoidLoc.getLocWithOffset(3));
|
|
|
|
diag(VoidLoc, Diagnostic) << FixItHint::CreateRemoval(VoidRange);
|
|
|
|
}
|
|
|
|
|
2016-04-15 03:28:13 +08:00
|
|
|
void RedundantVoidArgCheck::processTypedefNameDecl(
|
|
|
|
const MatchFinder::MatchResult &Result,
|
|
|
|
const TypedefNameDecl *TypedefName) {
|
|
|
|
if (protoTypeHasNoParms(TypedefName->getUnderlyingType())) {
|
|
|
|
removeVoidArgumentTokens(Result, TypedefName->getSourceRange(),
|
|
|
|
isa<TypedefDecl>(TypedefName) ? "typedef"
|
|
|
|
: "type alias");
|
2015-10-28 09:36:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processFieldDecl(
|
|
|
|
const MatchFinder::MatchResult &Result, const FieldDecl *Member) {
|
|
|
|
if (protoTypeHasNoParms(Member->getType())) {
|
|
|
|
removeVoidArgumentTokens(Result, Member->getSourceRange(),
|
|
|
|
"field declaration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processVarDecl(
|
|
|
|
const MatchFinder::MatchResult &Result, const VarDecl *Var) {
|
|
|
|
if (protoTypeHasNoParms(Var->getType())) {
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Begin = Var->getBeginLoc();
|
2015-10-28 09:36:20 +08:00
|
|
|
if (Var->hasInit()) {
|
|
|
|
SourceLocation InitStart =
|
2018-08-10 06:42:26 +08:00
|
|
|
Result.SourceManager->getExpansionLoc(Var->getInit()->getBeginLoc())
|
2015-10-28 09:36:20 +08:00
|
|
|
.getLocWithOffset(-1);
|
|
|
|
removeVoidArgumentTokens(Result, SourceRange(Begin, InitStart),
|
|
|
|
"variable declaration with initializer");
|
|
|
|
} else {
|
|
|
|
removeVoidArgumentTokens(Result, Var->getSourceRange(),
|
|
|
|
"variable declaration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processNamedCastExpr(
|
|
|
|
const MatchFinder::MatchResult &Result, const CXXNamedCastExpr *NamedCast) {
|
|
|
|
if (protoTypeHasNoParms(NamedCast->getTypeAsWritten())) {
|
|
|
|
removeVoidArgumentTokens(
|
|
|
|
Result,
|
|
|
|
NamedCast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange(),
|
|
|
|
"named cast");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processExplicitCastExpr(
|
|
|
|
const MatchFinder::MatchResult &Result,
|
|
|
|
const ExplicitCastExpr *ExplicitCast) {
|
|
|
|
if (protoTypeHasNoParms(ExplicitCast->getTypeAsWritten())) {
|
|
|
|
removeVoidArgumentTokens(Result, ExplicitCast->getSourceRange(),
|
|
|
|
"cast expression");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantVoidArgCheck::processLambdaExpr(
|
|
|
|
const MatchFinder::MatchResult &Result, const LambdaExpr *Lambda) {
|
|
|
|
if (Lambda->getLambdaClass()->getLambdaCallOperator()->getNumParams() == 0 &&
|
|
|
|
Lambda->hasExplicitParameters()) {
|
2018-08-10 21:59:33 +08:00
|
|
|
SourceManager *SM = Result.SourceManager;
|
|
|
|
TypeLoc TL = Lambda->getLambdaClass()->getLambdaTypeInfo()->getTypeLoc();
|
|
|
|
removeVoidArgumentTokens(Result,
|
|
|
|
{SM->getSpellingLoc(TL.getBeginLoc()),
|
|
|
|
SM->getSpellingLoc(TL.getEndLoc())},
|
2015-10-28 09:36:20 +08:00
|
|
|
"lambda expression");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|