Skip to content

Commit

Permalink
[clangd] Check for valid location in ExtractionContext::exprIsValidOu…
Browse files Browse the repository at this point in the history
…tside (#71162)

If the code has a call to an implicitly declared function, an expression
could end up referencing declarations without valid source locations. So
when doing the exprIsValidOutside check we could end up calling
SourceManager::isPointWithin using invalid source locations, and then a
debug build would crash with an assertion failure in
SourceManager::isBeforeInTranslationUnit.

This patch make sure that we deal with the invalid locations (by
considering a ReferencedDecl with invalid location as not being inside
the Scope).
  • Loading branch information
bjope committed Nov 11, 2023
1 parent 539c1c0 commit 5bd0f0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ bool ExtractionContext::exprIsValidOutside(const clang::Stmt *Scope) const {
SourceLocation ScopeBegin = Scope->getBeginLoc();
SourceLocation ScopeEnd = Scope->getEndLoc();
for (const Decl *ReferencedDecl : ReferencedDecls) {
if (SM.isPointWithin(ReferencedDecl->getBeginLoc(), ScopeBegin, ScopeEnd) &&
if (ReferencedDecl->getBeginLoc().isValid() &&
SM.isPointWithin(ReferencedDecl->getBeginLoc(), ScopeBegin, ScopeEnd) &&
SM.isPointWithin(ReferencedDecl->getEndLoc(), ScopeBegin, ScopeEnd))
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ TEST_F(ExtractVariableTest, Test) {
int x = [[1]];
})cpp";
EXPECT_AVAILABLE(AvailableC);

ExtraArgs = {"-xc"};
const char *NoCrashCasesC = R"cpp(
// error-ok: broken code, but shouldn't crash
int x = [[foo()]];
)cpp";
EXPECT_UNAVAILABLE(NoCrashCasesC);

ExtraArgs = {"-xobjective-c"};
const char *AvailableObjC = R"cpp(
__attribute__((objc_root_class))
Expand Down

0 comments on commit 5bd0f0d

Please sign in to comment.