MCPcopy Create free account
hub / github.com/andreasfertig/cppinsights / GetDeclContext

Function GetDeclContext

InsightsHelpers.cpp:326–389  ·  view source on GitHub ↗

own implementation due to lambdas

Source from the content-addressed store, hash-verified

324
325// own implementation due to lambdas
326std::string GetDeclContext(const DeclContext* ctx, WithTemplateParameters withTemplateParameters)
327{
328 OutputFormatHelper mOutputFormatHelper{};
329 SmallVector<const DeclContext*, 8> contexts{};
330
331 while(ctx) {
332 if(isa<NamedDecl>(ctx)) {
333 contexts.push_back(ctx);
334 }
335 ctx = ctx->getParent();
336 }
337
338 for(const auto* declContext : llvm::reverse(contexts)) {
339 if(const auto* classTmplSpec = dyn_cast<ClassTemplateSpecializationDecl>(declContext)) {
340 mOutputFormatHelper.Append(classTmplSpec->getName());
341
342 CodeGenerator codeGenerator{mOutputFormatHelper};
343 codeGenerator.InsertTemplateArgs(*classTmplSpec);
344
345 } else if(const auto* nd = dyn_cast<NamespaceDecl>(declContext)) {
346 if(nd->isAnonymousNamespace() or nd->isInline()) {
347 continue;
348 }
349
350 mOutputFormatHelper.Append(nd->getName());
351
352 } else if(const auto* rd = dyn_cast<RecordDecl>(declContext)) {
353 if(not rd->getIdentifier()) {
354 continue;
355 }
356
357 mOutputFormatHelper.Append(rd->getName());
358
359 // A special case at least for out-of-line static member variables of a class template. They need to carry
360 // the template parameters of the class template.
361 if(WithTemplateParameters::Yes == withTemplateParameters /*declContext->isNamespace() or declContext->getLexicalParent()->isNamespace() or declContext->getLexicalParent()->isTranslationUnit()*/) {
362 if(const auto* cxxRecordDecl = dyn_cast_or_null<CXXRecordDecl>(rd)) {
363 if(const auto* classTmpl = cxxRecordDecl->getDescribedClassTemplate()) {
364 CodeGenerator codeGenerator{mOutputFormatHelper};
365 codeGenerator.InsertTemplateParameters(*classTmpl->getTemplateParameters(),
366 CodeGenerator::TemplateParamsOnly::Yes);
367 }
368 }
369 }
370
371 } else if(dyn_cast<FunctionDecl>(declContext)) {
372 continue;
373
374 } else if(const auto* ed = dyn_cast<EnumDecl>(declContext)) {
375 if(not ed->isScoped()) {
376 continue;
377 }
378
379 mOutputFormatHelper.Append(ed->getName());
380
381 } else {
382 mOutputFormatHelper.Append(cast<NamedDecl>(declContext)->getName());
383 }

Callers 4

InsertArgMethod · 0.85
ParseDeclContextMethod · 0.85
GetQualifiedNameFunction · 0.85
HandleTypeMethod · 0.85

Calls 4

AppendMethod · 0.80
getNameMethod · 0.80
InsertTemplateArgsMethod · 0.45

Tested by

no test coverage detected