|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 |
|
14 | 14 | #include "clang/ExtractAPI/DeclarationFragments.h"
|
| 15 | +#include "clang/AST/Type.h" |
| 16 | +#include "clang/AST/TypeLoc.h" |
15 | 17 | #include "clang/ExtractAPI/TypedefUnderlyingTypeResolver.h"
|
16 | 18 | #include "clang/Index/USRGeneration.h"
|
17 | 19 | #include "llvm/ADT/StringSwitch.h"
|
18 | 20 |
|
19 | 21 | using namespace clang::extractapi;
|
20 | 22 | using namespace llvm;
|
21 | 23 |
|
| 24 | +namespace { |
| 25 | + |
| 26 | +void findTypeLocForBlockDecl(const clang::TypeSourceInfo *TSInfo, |
| 27 | + clang::FunctionTypeLoc &Block, |
| 28 | + clang::FunctionProtoTypeLoc &BlockProto) { |
| 29 | + if (!TSInfo) |
| 30 | + return; |
| 31 | + |
| 32 | + clang::TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 33 | + while (true) { |
| 34 | + // Look through qualified types |
| 35 | + if (auto QualifiedTL = TL.getAs<clang::QualifiedTypeLoc>()) { |
| 36 | + TL = QualifiedTL.getUnqualifiedLoc(); |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if (auto AttrTL = TL.getAs<clang::AttributedTypeLoc>()) { |
| 41 | + TL = AttrTL.getModifiedLoc(); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Try to get the function prototype behind the block pointer type, |
| 46 | + // then we're done. |
| 47 | + if (auto BlockPtr = TL.getAs<clang::BlockPointerTypeLoc>()) { |
| 48 | + TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 49 | + Block = TL.getAs<clang::FunctionTypeLoc>(); |
| 50 | + BlockProto = TL.getAs<clang::FunctionProtoTypeLoc>(); |
| 51 | + } |
| 52 | + break; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
22 | 58 | DeclarationFragments &DeclarationFragments::appendSpace() {
|
23 | 59 | if (!Fragments.empty()) {
|
24 | 60 | Fragment &Last = Fragments.back();
|
@@ -161,7 +197,7 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
|
161 | 197 |
|
162 | 198 | // Declaration fragments of a pointer type is the declaration fragments of
|
163 | 199 | // the pointee type followed by a `*`,
|
164 |
| - if (T->isPointerType()) |
| 200 | + if (T->isPointerType() && !T->isFunctionPointerType()) |
165 | 201 | return Fragments
|
166 | 202 | .append(getFragmentsForType(T->getPointeeType(), Context, After))
|
167 | 203 | .append(" *", DeclarationFragments::FragmentKind::Text);
|
@@ -371,46 +407,110 @@ DeclarationFragmentsBuilder::getFragmentsForVar(const VarDecl *Var) {
|
371 | 407 | .append(VarDecl::getStorageClassSpecifierString(SC),
|
372 | 408 | DeclarationFragments::FragmentKind::Keyword)
|
373 | 409 | .appendSpace();
|
374 |
| - QualType T = |
375 |
| - Var->getTypeSourceInfo() |
376 |
| - ? Var->getTypeSourceInfo()->getType() |
377 |
| - : Var->getASTContext().getUnqualifiedObjCPointerType(Var->getType()); |
378 | 410 |
|
379 | 411 | // Capture potential fragments that needs to be placed after the variable name
|
380 | 412 | // ```
|
381 | 413 | // int nums[5];
|
382 | 414 | // char (*ptr_to_array)[6];
|
383 | 415 | // ```
|
384 | 416 | DeclarationFragments After;
|
385 |
| - return Fragments.append(getFragmentsForType(T, Var->getASTContext(), After)) |
386 |
| - .appendSpace() |
| 417 | + FunctionTypeLoc BlockLoc; |
| 418 | + FunctionProtoTypeLoc BlockProtoLoc; |
| 419 | + findTypeLocForBlockDecl(Var->getTypeSourceInfo(), BlockLoc, BlockProtoLoc); |
| 420 | + |
| 421 | + if (!BlockLoc) { |
| 422 | + QualType T = Var->getTypeSourceInfo() |
| 423 | + ? Var->getTypeSourceInfo()->getType() |
| 424 | + : Var->getASTContext().getUnqualifiedObjCPointerType( |
| 425 | + Var->getType()); |
| 426 | + |
| 427 | + Fragments.append(getFragmentsForType(T, Var->getASTContext(), After)) |
| 428 | + .appendSpace(); |
| 429 | + } else { |
| 430 | + Fragments.append(getFragmentsForBlock(Var, BlockLoc, BlockProtoLoc, After)); |
| 431 | + } |
| 432 | + |
| 433 | + return Fragments |
387 | 434 | .append(Var->getName(), DeclarationFragments::FragmentKind::Identifier)
|
388 |
| - .append(std::move(After)); |
| 435 | + .append(std::move(After)) |
| 436 | + .append(";", DeclarationFragments::FragmentKind::Text); |
389 | 437 | }
|
390 | 438 |
|
391 | 439 | DeclarationFragments
|
392 | 440 | DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {
|
393 | 441 | DeclarationFragments Fragments, After;
|
394 | 442 |
|
395 |
| - QualType T = Param->getTypeSourceInfo() |
396 |
| - ? Param->getTypeSourceInfo()->getType() |
397 |
| - : Param->getASTContext().getUnqualifiedObjCPointerType( |
398 |
| - Param->getType()); |
| 443 | + auto *TSInfo = Param->getTypeSourceInfo(); |
399 | 444 |
|
400 |
| - DeclarationFragments TypeFragments = |
401 |
| - getFragmentsForType(T, Param->getASTContext(), After); |
| 445 | + QualType T = TSInfo ? TSInfo->getType() |
| 446 | + : Param->getASTContext().getUnqualifiedObjCPointerType( |
| 447 | + Param->getType()); |
402 | 448 |
|
403 |
| - if (Param->isObjCMethodParameter()) |
| 449 | + FunctionTypeLoc BlockLoc; |
| 450 | + FunctionProtoTypeLoc BlockProtoLoc; |
| 451 | + findTypeLocForBlockDecl(TSInfo, BlockLoc, BlockProtoLoc); |
| 452 | + |
| 453 | + DeclarationFragments TypeFragments; |
| 454 | + if (BlockLoc) |
| 455 | + TypeFragments.append( |
| 456 | + getFragmentsForBlock(Param, BlockLoc, BlockProtoLoc, After)); |
| 457 | + else |
| 458 | + TypeFragments.append(getFragmentsForType(T, Param->getASTContext(), After)); |
| 459 | + |
| 460 | + if (Param->isObjCMethodParameter()) { |
404 | 461 | Fragments.append("(", DeclarationFragments::FragmentKind::Text)
|
405 | 462 | .append(std::move(TypeFragments))
|
406 |
| - .append(") ", DeclarationFragments::FragmentKind::Text); |
407 |
| - else |
408 |
| - Fragments.append(std::move(TypeFragments)).appendSpace(); |
| 463 | + .append(std::move(After)) |
| 464 | + .append(") ", DeclarationFragments::FragmentKind::Text) |
| 465 | + .append(Param->getName(), |
| 466 | + DeclarationFragments::FragmentKind::InternalParam); |
| 467 | + } else { |
| 468 | + Fragments.append(std::move(TypeFragments)); |
| 469 | + if (!T->isBlockPointerType()) |
| 470 | + Fragments.appendSpace(); |
| 471 | + Fragments |
| 472 | + .append(Param->getName(), |
| 473 | + DeclarationFragments::FragmentKind::InternalParam) |
| 474 | + .append(std::move(After)); |
| 475 | + } |
| 476 | + return Fragments; |
| 477 | +} |
409 | 478 |
|
410 |
| - return Fragments |
411 |
| - .append(Param->getName(), |
412 |
| - DeclarationFragments::FragmentKind::InternalParam) |
413 |
| - .append(std::move(After)); |
| 479 | +DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForBlock( |
| 480 | + const NamedDecl *BlockDecl, FunctionTypeLoc &Block, |
| 481 | + FunctionProtoTypeLoc &BlockProto, DeclarationFragments &After) { |
| 482 | + DeclarationFragments Fragments; |
| 483 | + |
| 484 | + DeclarationFragments RetTyAfter; |
| 485 | + auto ReturnValueFragment = getFragmentsForType( |
| 486 | + Block.getTypePtr()->getReturnType(), BlockDecl->getASTContext(), After); |
| 487 | + |
| 488 | + Fragments.append(std::move(ReturnValueFragment)) |
| 489 | + .append(std::move(RetTyAfter)) |
| 490 | + .appendSpace() |
| 491 | + .append("(^", DeclarationFragments::FragmentKind::Text); |
| 492 | + |
| 493 | + After.append(")", DeclarationFragments::FragmentKind::Text); |
| 494 | + unsigned NumParams = Block.getNumParams(); |
| 495 | + |
| 496 | + if (!BlockProto || NumParams == 0) { |
| 497 | + if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
| 498 | + After.append("(...)", DeclarationFragments::FragmentKind::Text); |
| 499 | + else |
| 500 | + After.append("()", DeclarationFragments::FragmentKind::Text); |
| 501 | + } else { |
| 502 | + After.append("(", DeclarationFragments::FragmentKind::Text); |
| 503 | + for (unsigned I = 0; I != NumParams; ++I) { |
| 504 | + if (I) |
| 505 | + After.append(", ", DeclarationFragments::FragmentKind::Text); |
| 506 | + After.append(getFragmentsForParam(Block.getParam(I))); |
| 507 | + if (I == NumParams - 1 && BlockProto.getTypePtr()->isVariadic()) |
| 508 | + After.append(", ...", DeclarationFragments::FragmentKind::Text); |
| 509 | + } |
| 510 | + After.append(")", DeclarationFragments::FragmentKind::Text); |
| 511 | + } |
| 512 | + |
| 513 | + return Fragments; |
414 | 514 | }
|
415 | 515 |
|
416 | 516 | DeclarationFragments
|
@@ -445,12 +545,19 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {
|
445 | 545 | .append(std::move(After));
|
446 | 546 |
|
447 | 547 | Fragments.append("(", DeclarationFragments::FragmentKind::Text);
|
448 |
| - for (unsigned i = 0, end = Func->getNumParams(); i != end; ++i) { |
| 548 | + unsigned NumParams = Func->getNumParams(); |
| 549 | + for (unsigned i = 0; i != NumParams; ++i) { |
449 | 550 | if (i)
|
450 | 551 | Fragments.append(", ", DeclarationFragments::FragmentKind::Text);
|
451 | 552 | Fragments.append(getFragmentsForParam(Func->getParamDecl(i)));
|
452 | 553 | }
|
453 |
| - Fragments.append(")", DeclarationFragments::FragmentKind::Text); |
| 554 | + |
| 555 | + if (Func->isVariadic()) { |
| 556 | + if (NumParams > 0) |
| 557 | + Fragments.append(", ", DeclarationFragments::FragmentKind::Text); |
| 558 | + Fragments.append("...", DeclarationFragments::FragmentKind::Text); |
| 559 | + } |
| 560 | + Fragments.append(");", DeclarationFragments::FragmentKind::Text); |
454 | 561 |
|
455 | 562 | // FIXME: Handle exception specifiers: throw, noexcept
|
456 | 563 | return Fragments;
|
@@ -705,14 +812,27 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForObjCProperty(
|
705 | 812 | Fragments.append(")", DeclarationFragments::FragmentKind::Text);
|
706 | 813 | }
|
707 | 814 |
|
708 |
| - // Build the property type and name, and return the completed fragments. |
709 |
| - return Fragments.appendSpace() |
710 |
| - .append(getFragmentsForType(Property->getType(), |
711 |
| - Property->getASTContext(), After)) |
712 |
| - .appendSpace() |
| 815 | + Fragments.appendSpace(); |
| 816 | + |
| 817 | + FunctionTypeLoc BlockLoc; |
| 818 | + FunctionProtoTypeLoc BlockProtoLoc; |
| 819 | + findTypeLocForBlockDecl(Property->getTypeSourceInfo(), BlockLoc, |
| 820 | + BlockProtoLoc); |
| 821 | + |
| 822 | + auto PropType = Property->getType(); |
| 823 | + if (!BlockLoc) |
| 824 | + Fragments |
| 825 | + .append(getFragmentsForType(PropType, Property->getASTContext(), After)) |
| 826 | + .appendSpace(); |
| 827 | + else |
| 828 | + Fragments.append( |
| 829 | + getFragmentsForBlock(Property, BlockLoc, BlockProtoLoc, After)); |
| 830 | + |
| 831 | + return Fragments |
713 | 832 | .append(Property->getName(),
|
714 | 833 | DeclarationFragments::FragmentKind::Identifier)
|
715 |
| - .append(std::move(After)); |
| 834 | + .append(std::move(After)) |
| 835 | + .append(";", DeclarationFragments::FragmentKind::Text); |
716 | 836 | }
|
717 | 837 |
|
718 | 838 | DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForObjCProtocol(
|
|
0 commit comments