Skip to content

Commit

Permalink
Fix crash in redundant-void-arg check.
Browse files Browse the repository at this point in the history
Summary:
When applying this check to the unit tests, it would hit an assertion:
llvm/tools/clang/lib/Lex/Lexer.cpp:1056: clang::SourceLocation clang::Lexer::getSourceLocation(const char*, unsigned int) const: Assertion `PP && "This doesn't work on raw lexers"' failed.

Reviewers: klimek, LegalizeAdulthood, alexfh

Subscribers: cfe-commits, alexfh

Differential Revision: http://reviews.llvm.org/D14204

llvm-svn: 251792
  • Loading branch information
angargo committed Nov 2, 2015
1 parent 8286b83 commit dd4ed3a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
24 changes: 14 additions & 10 deletions clang-tools-extra/clang-tidy/modernize/RedundantVoidArgCheck.cpp
Expand Up @@ -47,8 +47,8 @@ namespace modernize {

void RedundantVoidArgCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(functionDecl(isExpansionInMainFile(), parameterCountIs(0),
unless(isImplicit()),
unless(isExternC())).bind(FunctionId),
unless(isImplicit()), unless(isExternC()))
.bind(FunctionId),
this);
Finder->addMatcher(typedefDecl(isExpansionInMainFile()).bind(TypedefId),
this);
Expand Down Expand Up @@ -77,9 +77,10 @@ void RedundantVoidArgCheck::registerMatchers(MatchFinder *Finder) {
cxxReinterpretCastExpr(isExpansionInMainFile(), CastDestinationIsFunction)
.bind(NamedCastId),
this);
Finder->addMatcher(cxxConstCastExpr(isExpansionInMainFile(),
CastDestinationIsFunction).bind(NamedCastId),
this);
Finder->addMatcher(
cxxConstCastExpr(isExpansionInMainFile(), CastDestinationIsFunction)
.bind(NamedCastId),
this);
Finder->addMatcher(lambdaExpr(isExpansionInMainFile()).bind(LambdaId), this);
}

Expand Down Expand Up @@ -128,11 +129,14 @@ void RedundantVoidArgCheck::processFunctionDecl(
void RedundantVoidArgCheck::removeVoidArgumentTokens(
const ast_matchers::MatchFinder::MatchResult &Result, SourceRange Range,
StringRef GrammarLocation) {
std::string DeclText =
Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
*Result.SourceManager,
Result.Context->getLangOpts()).str();
Lexer PrototypeLexer(Range.getBegin(), Result.Context->getLangOpts(),
CharSourceRange CharRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Range), *Result.SourceManager,
Result.Context->getLangOpts());

std::string DeclText = Lexer::getSourceText(CharRange, *Result.SourceManager,
Result.Context->getLangOpts())
.str();
Lexer PrototypeLexer(CharRange.getBegin(), Result.Context->getLangOpts(),
DeclText.data(), DeclText.data(),
DeclText.data() + DeclText.size());
enum TokenState {
Expand Down
16 changes: 16 additions & 0 deletions clang-tools-extra/test/clang-tidy/modernize-redundant-void-arg.cpp
Expand Up @@ -417,3 +417,19 @@ void test_lambda_functions() {
// CHECK-MESSAGES: [[@LINE-2]]:45: warning: {{.*}} in lambda expression
// CHECK-FIXES: {{^ }}auto void_returner = []() -> void (*)() { return f1; };{{$}}
}

#define M(x) x

M(void inmacro(void) {})
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: {{.*}} in function definition
// CHECK-FIXES: M(void inmacro() {})

#define F(A, B) \
struct F_##A##_##B { \
F_##A##_##B(void); \
}; \
F_##A##_##B::F_##A##_##B(void)

F(Foo, Bar) {

}

0 comments on commit dd4ed3a

Please sign in to comment.