Skip to content

Commit d623a06

Browse files
kadircetsam-mccall
authored andcommitted
[clangd] Make use of SourceOrder to find first initializer in DefineOutline
Summary: Constructors can have implicit initializers, this was crashing define outline. Make sure we find the first "written" ctor initializer to figure out `:` location. Fixes clangd/clangd#400 Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D80521 (cherry picked from commit eeedbd0) Fixes clangd/clangd#418
1 parent cb89646 commit d623a06

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,16 @@ SourceRange getDeletionRange(const FunctionDecl *FD,
306306
const syntax::TokenBuffer &TokBuf) {
307307
auto DeletionRange = FD->getBody()->getSourceRange();
308308
if (auto *CD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
309-
const auto &SM = TokBuf.sourceManager();
310309
// AST doesn't contain the location for ":" in ctor initializers. Therefore
311310
// we find it by finding the first ":" before the first ctor initializer.
312311
SourceLocation InitStart;
313312
// Find the first initializer.
314313
for (const auto *CInit : CD->inits()) {
315-
// We don't care about in-class initializers.
316-
if (CInit->isInClassMemberInitializer())
314+
// SourceOrder is -1 for implicit initializers.
315+
if (CInit->getSourceOrder() != 0)
317316
continue;
318-
if (InitStart.isInvalid() ||
319-
SM.isBeforeInTranslationUnit(CInit->getSourceLocation(), InitStart))
320-
InitStart = CInit->getSourceLocation();
317+
InitStart = CInit->getSourceLocation();
318+
break;
321319
}
322320
if (InitStart.isValid()) {
323321
auto Toks = TokBuf.expandedTokens(CD->getSourceRange());

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,21 +2047,57 @@ TEST_F(DefineOutlineTest, ApplyTest) {
20472047
"void foo(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) ;",
20482048
"void foo(int x, int y , int , int (*foo)(int) ) {}",
20492049
},
2050-
// Ctor initializers.
2050+
// Constructors
2051+
{
2052+
R"cpp(
2053+
class Foo {public: Foo(); Foo(int);};
2054+
class Bar {
2055+
Ba^r() {}
2056+
Bar(int x) : f1(x) {}
2057+
Foo f1;
2058+
Foo f2 = 2;
2059+
};)cpp",
2060+
R"cpp(
2061+
class Foo {public: Foo(); Foo(int);};
2062+
class Bar {
2063+
Bar() ;
2064+
Bar(int x) : f1(x) {}
2065+
Foo f1;
2066+
Foo f2 = 2;
2067+
};)cpp",
2068+
"Bar::Bar() {}\n",
2069+
},
2070+
// Ctor with initializer.
2071+
{
2072+
R"cpp(
2073+
class Foo {public: Foo(); Foo(int);};
2074+
class Bar {
2075+
Bar() {}
2076+
B^ar(int x) : f1(x), f2(3) {}
2077+
Foo f1;
2078+
Foo f2 = 2;
2079+
};)cpp",
2080+
R"cpp(
2081+
class Foo {public: Foo(); Foo(int);};
2082+
class Bar {
2083+
Bar() {}
2084+
Bar(int x) ;
2085+
Foo f1;
2086+
Foo f2 = 2;
2087+
};)cpp",
2088+
"Bar::Bar(int x) : f1(x), f2(3) {}\n",
2089+
},
2090+
// Ctor initializer with attribute.
20512091
{
20522092
R"cpp(
20532093
class Foo {
2054-
int y = 2;
20552094
F^oo(int z) __attribute__((weak)) : bar(2){}
20562095
int bar;
2057-
int z = 2;
20582096
};)cpp",
20592097
R"cpp(
20602098
class Foo {
2061-
int y = 2;
20622099
Foo(int z) __attribute__((weak)) ;
20632100
int bar;
2064-
int z = 2;
20652101
};)cpp",
20662102
"Foo::Foo(int z) __attribute__((weak)) : bar(2){}\n",
20672103
},

0 commit comments

Comments
 (0)