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