@@ -262,35 +262,42 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
262
262
std::string MethodString;
263
263
llvm::raw_string_ostream OS (MethodString);
264
264
265
+ PrintingPolicy PP = Context.getPrintingPolicy ();
266
+ PP.PolishForDeclaration = true ;
267
+ PP.SupressStorageClassSpecifiers = true ;
268
+ PP.SuppressStrongLifetime = true ;
269
+ PP.SuppressLifetimeQualifiers = true ;
270
+ PP.SuppressUnwrittenScope = true ;
271
+
272
+ // Callback class for skipping namespaces.
273
+ class Callbacks final : public PrintingCallbacks {
274
+ public:
275
+ bool isScopeVisible (const DeclContext *DC) const override {
276
+ return DC->getDeclKind () == Decl::Namespace;
277
+ }
278
+ } CB;
279
+
265
280
// Pick a good insertion location.
266
281
SourceLocation InsertionLoc;
267
282
const CXXMethodDecl *InsertAfterMethod = nullptr ;
268
- std::optional<NestedNameSpecifier> NamePrefix = std::nullopt ;
269
283
if (DefinedOutOfLineMethods.empty ()) {
284
+ PP.Callbacks = &CB;
270
285
const RecordDecl *OutermostRecord = findOutermostRecord (Class);
271
286
InsertionLoc = SM.getExpansionRange (OutermostRecord->getEndLoc ()).getEnd ();
272
287
if (SM.getFileID (InsertionLoc) == File) {
273
- // We can insert right after the class. Compute the appropriate
274
- // qualification.
275
- NamePrefix = NestedNameSpecifier::getRequiredQualification (
276
- Context, OutermostRecord->getLexicalDeclContext (),
277
- Class->getLexicalDeclContext ());
288
+ // We can insert right after the class.
278
289
} else {
279
290
// We can't insert after the end of the class, since the indexer told us
280
291
// that some file should have the implementation of it, even when there
281
292
// are no methods here. We should try to insert at the end of the file.
282
293
InsertionLoc = SM.getLocForEndOfFile (File);
283
- NamePrefix = NestedNameSpecifier::getRequiredQualification (
284
- Context, Context.getTranslationUnitDecl (),
285
- Class->getLexicalDeclContext ());
286
294
llvm::SmallVector<const NamespaceDecl *, 4 > Namespaces;
287
- std::optional<NestedNameSpecifier> Qualifier = NamePrefix;
288
- while (Qualifier) {
289
- auto [ND, Prefix] = Qualifier->getAsNamespaceAndPrefix ();
290
- if (ND)
291
- Namespaces.push_back (ND->getNamespace ());
292
- Qualifier = Prefix;
293
- }
295
+
296
+ for (const DeclContext *DC = OutermostRecord->getLexicalDeclContext (); DC;
297
+ DC = DC->getLookupParent ())
298
+ if (auto *ND = dyn_cast<NamespaceDecl>(DC))
299
+ Namespaces.push_back (ND);
300
+
294
301
// When the class is in a namespace, add a 'using' declaration if it's
295
302
// needed and adjust the out-of-line qualification.
296
303
if (!Namespaces.empty ()) {
@@ -305,9 +312,6 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
305
312
}
306
313
OS << " \n using namespace " << NamespaceOS.str () << " ;" ;
307
314
}
308
- // Re-compute the name qualifier without the namespace.
309
- NamePrefix = NestedNameSpecifier::getRequiredQualification (
310
- Context, InnermostNamespace, Class->getLexicalDeclContext ());
311
315
}
312
316
}
313
317
} else {
@@ -324,12 +328,6 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
324
328
InsertionLoc = getLastLineLocationUnlessItHasOtherTokens (
325
329
InsertionLoc, SM, Context.getLangOpts ());
326
330
327
- PrintingPolicy PP = Context.getPrintingPolicy ();
328
- PP.PolishForDeclaration = true ;
329
- PP.SupressStorageClassSpecifiers = true ;
330
- PP.SuppressStrongLifetime = true ;
331
- PP.SuppressLifetimeQualifiers = true ;
332
- PP.SuppressUnwrittenScope = true ;
333
331
OS << " \n " ;
334
332
for (const auto &I : SelectedMethods) {
335
333
const CXXMethodDecl *MD = I.Decl ;
0 commit comments