33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/PointerUnion.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/ADT/ScopeExit.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/Support/Allocator.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/FormatVariadic.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/raw_ostream.h"
48 using namespace clang;
54 if (
auto *C = dyn_cast<CXXConstructExpr>(E)) {
55 auto NumArgs = C->getNumArgs();
56 if (NumArgs == 1 || (NumArgs > 1 && isa<CXXDefaultArgExpr>(C->getArg(1)))) {
57 Expr *A = C->getArg(0);
58 if (C->getParenOrBraceRange().isInvalid())
73 if (
auto *F = dyn_cast<CXXFunctionalCastExpr>(E)) {
74 if (F->getCastKind() == CK_ConstructorConversion)
75 return F->getSubExpr();
102 struct GetStartLoc :
TypeLocVisitor<GetStartLoc, SourceLocation> {
104 auto L = Visit(
T.getInnerLoc());
107 return T.getLParenLoc();
112 return HandlePointer(
T);
116 return HandlePointer(
T);
120 return HandlePointer(
T);
124 return HandlePointer(
T);
128 return HandlePointer(
T);
136 auto N =
T.getNextTypeLoc();
145 return VisitTypeLoc(
T);
150 auto L = Visit(
T.getPointeeLoc());
153 return T.getLocalSourceRange().getBegin();
159 auto FirstDefaultArg =
160 llvm::find_if(Args, [](
auto It) {
return isa<CXXDefaultArgExpr>(It); });
161 return llvm::make_range(Args.begin(), FirstDefaultArg);
168 case OO_ExclaimEqual:
170 case OO_GreaterEqual:
177 case OO_PercentEqual:
180 case OO_LessLessEqual:
181 case OO_GreaterGreaterEqual:
192 case OO_GreaterGreater:
197 return syntax::NodeKind::BinaryOperatorExpression;
200 return syntax::NodeKind::PrefixUnaryOperatorExpression;
206 return syntax::NodeKind::PrefixUnaryOperatorExpression;
208 return syntax::NodeKind::PostfixUnaryOperatorExpression;
210 llvm_unreachable(
"Invalid number of arguments for operator");
219 return syntax::NodeKind::PrefixUnaryOperatorExpression;
221 return syntax::NodeKind::BinaryOperatorExpression;
223 llvm_unreachable(
"Invalid number of arguments for operator");
225 return syntax::NodeKind::BinaryOperatorExpression;
230 case OO_Array_Delete:
234 return syntax::NodeKind::UnknownExpression;
236 return syntax::NodeKind::CallExpression;
240 llvm_unreachable(
"Not an overloadable operator");
242 llvm_unreachable(
"Unknown OverloadedOperatorKind enum");
251 assert((isa<DeclaratorDecl, TypedefNameDecl>(D)) &&
252 "only DeclaratorDecl and TypedefNameDecl are supported.");
255 bool IsAnonymous = DN.
isIdentifier() && !DN.getAsIdentifierInfo();
259 if (
const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
260 if (DD->getQualifierLoc()) {
261 return DD->getQualifierLoc().getBeginLoc();
274 if (
auto *
V = dyn_cast<VarDecl>(D)) {
275 auto *I =
V->getInit();
277 if (I && !
V->isCXXForRangeDecl())
278 return I->getSourceRange();
297 if (Name.isValid()) {
307 assert(
SM.isBeforeInTranslationUnit(
End, InitializerEnd) ||
308 End == InitializerEnd);
309 End = InitializerEnd;
316 using ASTPtr = llvm::PointerUnion<Stmt *, Decl *>;
320 class ASTToSyntaxMapping {
323 assert(To !=
nullptr);
324 assert(!From.isNull());
326 bool Added =
Nodes.insert({From, To}).second;
328 assert(Added &&
"mapping added twice");
332 assert(To !=
nullptr);
335 bool Added = NNSNodes.insert({From, To}).second;
337 assert(Added &&
"mapping added twice");
343 return NNSNodes.lookup(
P);
347 llvm::DenseMap<ASTPtr, syntax::Tree *>
Nodes;
348 llvm::DenseMap<NestedNameSpecifierLoc, syntax::Tree *> NNSNodes;
372 Pending(
Arena, TBTM.tokenBuffer()) {
374 LocationToToken.insert({
T.location(), &
T});
379 return TBTM.sourceManager();
386 Pending.foldChildren(TBTM.tokenBuffer(),
Range, New);
388 Mapping.add(From, New);
399 Pending.foldChildren(TBTM.tokenBuffer(),
Range, New);
401 Mapping.add(From, New);
409 auto ListRange = Pending.shrinkToFitList(SuperRange);
410 Pending.foldChildren(TBTM.tokenBuffer(), ListRange, New);
412 Mapping.add(From, New);
441 auto Tokens = TBTM.tokenBuffer().expandedTokens();
442 assert(!Tokens.empty());
443 assert(Tokens.back().kind() ==
tok::eof);
446 Pending.foldChildren(TBTM.tokenBuffer(), Tokens.drop_back(),
449 auto *TU = cast<syntax::TranslationUnit>(std::move(Pending).
finalize());
450 TU->assertInvariantsRecursive();
468 assert(
First.isValid());
469 assert(
Last.isValid());
471 TBTM.sourceManager().isBeforeInTranslationUnit(
First,
Last));
478 return maybeAppendSemicolon(Tokens, D);
484 assert((isa<DeclaratorDecl, TypedefNameDecl>(D)) &&
485 "only DeclaratorDecl and TypedefNameDecl are supported.");
490 if (Next ==
nullptr) {
495 if (D->
getKind() != Next->getKind()) {
512 if (
const auto *S = dyn_cast<TagDecl>(D))
513 Tokens =
getRange(S->TypeDecl::getBeginLoc(), S->getEndLoc());
516 return maybeAppendSemicolon(Tokens, D);
526 auto Tokens =
getRange(S->getSourceRange());
527 if (isa<CompoundStmt>(S))
532 if (Tokens.back().kind() == tok::semi)
534 return withTrailingSemicolon(Tokens);
539 const Decl *D)
const {
540 if (isa<NamespaceDecl>(D))
542 if (DeclsWithoutSemicolons.count(D))
546 return withTrailingSemicolon(Tokens);
551 assert(!Tokens.empty());
552 assert(Tokens.back().kind() !=
tok::eof);
554 if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi)
581 Trees.insert(Trees.end(), {&
T, L});
586 assert(!
Range.empty());
587 auto It = Trees.lower_bound(
Range.begin());
588 assert(It != Trees.end() &&
"no node found");
589 assert(It->first ==
Range.begin() &&
"no child with the specified range");
590 assert((std::next(It) == Trees.end() ||
591 std::next(It)->first ==
Range.end()) &&
592 "no child with the specified range");
594 "re-assigning role for a child");
595 It->second->setRole(Role);
601 auto BeginChildren = Trees.lower_bound(
Range.begin());
602 assert((BeginChildren == Trees.end() ||
603 BeginChildren->first ==
Range.begin()) &&
604 "Range crosses boundaries of existing subtrees");
606 auto EndChildren = Trees.lower_bound(
Range.end());
608 (EndChildren == Trees.end() || EndChildren->first ==
Range.end()) &&
609 "Range crosses boundaries of existing subtrees");
611 auto BelongsToList = [](decltype(Trees)::value_type KV) {
612 auto Role = KV.second->getRole();
617 auto BeginListChildren =
618 std::find_if(BeginChildren, EndChildren, BelongsToList);
620 auto EndListChildren =
621 std::find_if_not(BeginListChildren, EndChildren, BelongsToList);
624 EndListChildren->first);
631 assert(
Node->getFirstChild() ==
nullptr &&
"node already has children");
633 auto *FirstToken = Tokens.begin();
634 auto BeginChildren = Trees.lower_bound(FirstToken);
636 assert((BeginChildren == Trees.end() ||
637 BeginChildren->first == FirstToken) &&
638 "fold crosses boundaries of existing subtrees");
639 auto EndChildren = Trees.lower_bound(Tokens.end());
641 (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) &&
642 "fold crosses boundaries of existing subtrees");
644 for (
auto It = BeginChildren; It != EndChildren; ++It) {
645 auto *
C = It->second;
648 Node->appendChildLowLevel(C);
652 Node->Original =
true;
656 Trees.erase(BeginChildren, EndChildren);
657 Trees.insert({FirstToken,
Node});
662 assert(Trees.size() == 1);
663 auto *Root = Trees.begin()->second;
670 for (
auto It = Trees.begin(); It != Trees.end(); ++It) {
671 unsigned CoveredTokens =
673 ? (std::next(It)->first - It->first)
677 formatv(
"- '{0}' covers '{1}'+{2} tokens\n", It->second->getKind(),
679 R += It->second->dump(STM);
688 std::map<const syntax::Token *, syntax::Node *> Trees;
692 std::string str() {
return Pending.str(TBTM); }
695 TokenBufferTokenManager& TBTM;
697 llvm::DenseMap<SourceLocation, const syntax::Token *> LocationToToken;
700 ASTToSyntaxMapping Mapping;
707 : Builder(Builder), Context(Context) {}
709 bool shouldTraversePostOrder()
const {
return true; }
712 return processDeclaratorAndDeclaration(DD);
716 return processDeclaratorAndDeclaration(TD);
719 bool VisitDecl(
Decl *D) {
721 Builder.foldNode(Builder.getDeclarationRange(D),
731 if (!RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(C))
733 if (
C->isExplicitSpecialization())
736 cast<syntax::SimpleDeclaration>(handleFreeStandingTagDecl(C));
737 foldExplicitTemplateInstantiation(
738 Builder.getTemplateRange(C),
739 Builder.findToken(
C->getExternKeywordLoc()),
740 Builder.findToken(
C->getTemplateKeywordLoc()), Declaration, C);
745 foldTemplateDeclaration(
746 Builder.getDeclarationRange(S),
747 Builder.findToken(S->getTemplateParameters()->getTemplateLoc()),
748 Builder.getDeclarationRange(S->getTemplatedDecl()), S);
752 bool WalkUpFromTagDecl(
TagDecl *C) {
754 if (!
C->isFreeStanding()) {
755 assert(
C->getNumTemplateParameterLists() == 0);
758 handleFreeStandingTagDecl(C);
763 assert(
C->isFreeStanding());
765 auto DeclarationRange = Builder.getDeclarationRange(C);
767 Builder.foldNode(DeclarationRange, Result,
nullptr);
771 const auto *TemplateKW = Builder.findToken(L.getTemplateLoc());
774 foldTemplateDeclaration(R, TemplateKW, DeclarationRange,
nullptr);
775 DeclarationRange = R;
777 if (
auto *S = dyn_cast<ClassTemplatePartialSpecializationDecl>(C))
778 ConsumeTemplateParameters(*S->getTemplateParameters());
779 for (
unsigned I =
C->getNumTemplateParameterLists(); 0 < I; --I)
780 ConsumeTemplateParameters(*
C->getTemplateParameterList(I - 1));
793 Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen);
794 for (
auto *Child : S->body())
795 Builder.markStmtChild(Child, NodeRole::Statement);
796 Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen);
798 Builder.foldNode(Builder.getStmtRange(S),
804 bool WalkUpFromStmt(
Stmt *S) {
805 Builder.foldNode(Builder.getStmtRange(S),
810 bool TraverseIfStmt(
IfStmt *S) {
811 bool Result = [&,
this]() {
812 if (S->getInit() && !TraverseStmt(S->getInit())) {
818 if (S->hasVarStorage()) {
819 if (!TraverseStmt(S->getConditionVariableDeclStmt()))
821 }
else if (S->getCond() && !TraverseStmt(S->getCond()))
824 if (S->getThen() && !TraverseStmt(S->getThen()))
826 if (S->getElse() && !TraverseStmt(S->getElse()))
839 bool Result = [&,
this]() {
840 if (S->getInit() && !TraverseStmt(S->getInit()))
842 if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable()))
844 if (S->getRangeInit() && !TraverseStmt(S->getRangeInit()))
846 if (S->getBody() && !TraverseStmt(S->getBody()))
850 WalkUpFromCXXForRangeStmt(S);
854 bool TraverseStmt(
Stmt *S) {
855 if (
auto *DS = dyn_cast_or_null<DeclStmt>(S)) {
857 for (
auto *D : DS->decls())
858 Builder.noticeDeclWithoutSemicolon(D);
859 }
else if (
auto *E = dyn_cast_or_null<Expr>(S)) {
871 bool WalkUpFromExpr(
Expr *E) {
872 assert(!
isImplicitExpr(E) &&
"should be handled by TraverseStmt");
873 Builder.foldNode(Builder.getExprRange(E),
884 return WalkUpFromUserDefinedLiteral(S);
887 syntax::UserDefinedLiteralExpression *
889 switch (S->getLiteralOperatorKind()) {
891 return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
893 return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
895 return new (allocator()) syntax::CharUserDefinedLiteralExpression;
897 return new (allocator()) syntax::StringUserDefinedLiteralExpression;
904 auto TokLoc = S->getBeginLoc();
911 if (
Literal.isIntegerLiteral())
912 return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
914 assert(
Literal.isFloatingLiteral());
915 return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
918 llvm_unreachable(
"Unknown literal operator kind.");
923 Builder.foldNode(Builder.getExprRange(S), buildUserDefinedLiteral(S), S);
942 if (
auto DependentTL =
947 SR.
setBegin(DependentTL.getTemplateKeywordLoc());
957 return syntax::NodeKind::GlobalNameSpecifier;
961 return syntax::NodeKind::IdentifierNameSpecifier;
963 return syntax::NodeKind::SimpleTemplateNameSpecifier;
967 if (isa<DecltypeType>(NNSType))
968 return syntax::NodeKind::DecltypeNameSpecifier;
969 if (isa<TemplateSpecializationType, DependentTemplateSpecializationType>(
971 return syntax::NodeKind::SimpleTemplateNameSpecifier;
972 return syntax::NodeKind::IdentifierNameSpecifier;
976 llvm::report_fatal_error(
"We don't yet support the __super specifier",
981 syntax::NameSpecifier *
984 auto NameSpecifierTokens =
985 Builder.getRange(getLocalSourceRange(NNSLoc)).drop_back();
987 case syntax::NodeKind::GlobalNameSpecifier:
988 return new (allocator()) syntax::GlobalNameSpecifier;
989 case syntax::NodeKind::IdentifierNameSpecifier: {
990 assert(NameSpecifierTokens.size() == 1);
991 Builder.markChildToken(NameSpecifierTokens.begin(),
993 auto *NS =
new (allocator()) syntax::IdentifierNameSpecifier;
994 Builder.foldNode(NameSpecifierTokens, NS,
nullptr);
997 case syntax::NodeKind::SimpleTemplateNameSpecifier: {
1003 auto *NS =
new (allocator()) syntax::SimpleTemplateNameSpecifier;
1004 Builder.foldNode(NameSpecifierTokens, NS,
nullptr);
1007 case syntax::NodeKind::DecltypeNameSpecifier: {
1009 if (!RecursiveASTVisitor::TraverseDecltypeTypeLoc(TL))
1011 auto *NS =
new (allocator()) syntax::DecltypeNameSpecifier;
1016 Builder.foldNode(NameSpecifierTokens, NS,
nullptr);
1020 llvm_unreachable(
"getChildKind() does not return this value");
1031 for (
auto It = QualifierLoc; It; It = It.getPrefix()) {
1032 auto *NS = buildNameSpecifier(It);
1038 Builder.foldNode(Builder.getRange(QualifierLoc.
getSourceRange()),
1050 if (TemplateKeywordLoc.
isValid())
1051 Builder.markChildToken(TemplateKeywordLoc,
1056 Builder.foldNode(Builder.getRange(UnqualifiedIdLoc), TheUnqualifiedId,
1060 auto IdExpressionBeginLoc =
1063 auto *TheIdExpression =
new (allocator()) syntax::IdExpression;
1065 Builder.getRange(IdExpressionBeginLoc, UnqualifiedIdLoc.
getEnd()),
1066 TheIdExpression, From);
1068 return TheIdExpression;
1075 if (S->isImplicitAccess()) {
1076 buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1077 SourceRange(S->getMemberLoc(), S->getEndLoc()), S);
1081 auto *TheIdExpression = buildIdExpression(
1082 S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1083 SourceRange(S->getMemberLoc(), S->getEndLoc()),
nullptr);
1090 Builder.foldNode(Builder.getExprRange(S),
1091 new (allocator()) syntax::MemberExpression, S);
1096 buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1097 SourceRange(S->getLocation(), S->getEndLoc()), S);
1104 buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1105 SourceRange(S->getLocation(), S->getEndLoc()), S);
1111 if (!S->isImplicit()) {
1112 Builder.markChildToken(S->getLocation(),
1114 Builder.foldNode(Builder.getExprRange(S),
1115 new (allocator()) syntax::ThisExpression, S);
1120 bool WalkUpFromParenExpr(
ParenExpr *S) {
1124 Builder.foldNode(Builder.getExprRange(S),
1125 new (allocator()) syntax::ParenExpression, S);
1131 Builder.foldNode(Builder.getExprRange(S),
1132 new (allocator()) syntax::IntegerLiteralExpression, S);
1138 Builder.foldNode(Builder.getExprRange(S),
1139 new (allocator()) syntax::CharacterLiteralExpression, S);
1145 Builder.foldNode(Builder.getExprRange(S),
1146 new (allocator()) syntax::FloatingLiteralExpression, S);
1152 Builder.foldNode(Builder.getExprRange(S),
1153 new (allocator()) syntax::StringLiteralExpression, S);
1159 Builder.foldNode(Builder.getExprRange(S),
1160 new (allocator()) syntax::BoolLiteralExpression, S);
1166 Builder.foldNode(Builder.getExprRange(S),
1167 new (allocator()) syntax::CxxNullPtrExpression, S);
1172 Builder.markChildToken(S->getOperatorLoc(),
1177 Builder.foldNode(Builder.getExprRange(S),
1181 Builder.foldNode(Builder.getExprRange(S),
1190 Builder.markChildToken(S->getOperatorLoc(),
1193 Builder.foldNode(Builder.getExprRange(S),
1203 for (
auto *Arg : Args) {
1205 const auto *DelimiterToken =
1206 std::next(Builder.findToken(Arg->getEndLoc()));
1207 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1213 Builder.foldNode(Builder.getRange((*Args.begin())->getBeginLoc(),
1214 (*(Args.end() - 1))->getEndLoc()),
1220 bool WalkUpFromCallExpr(
CallExpr *S) {
1223 const auto *LParenToken =
1224 std::next(Builder.findToken(S->getCallee()->getEndLoc()));
1227 if (LParenToken->kind() == clang::tok::l_paren)
1230 Builder.markChild(buildCallArguments(S->arguments()),
1235 Builder.foldNode(Builder.getRange(S->getSourceRange()),
1236 new (allocator()) syntax::CallExpression, S);
1242 if ((S->getNumArgs() == 0 || isa<CXXDefaultArgExpr>(S->getArg(0))) &&
1243 S->getParenOrBraceRange().isInvalid())
1245 return RecursiveASTVisitor::WalkUpFromCXXConstructExpr(S);
1253 for (
auto *child : S->arguments()) {
1260 if (child->getSourceRange().isInvalid()) {
1262 syntax::NodeKind::PostfixUnaryOperatorExpression);
1265 if (!TraverseStmt(child))
1268 return WalkUpFromCXXOperatorCallExpr(S);
1273 case syntax::NodeKind::BinaryOperatorExpression:
1275 Builder.markChildToken(S->getOperatorLoc(),
1278 Builder.foldNode(Builder.getExprRange(S),
1281 case syntax::NodeKind::PrefixUnaryOperatorExpression:
1282 Builder.markChildToken(S->getOperatorLoc(),
1285 Builder.foldNode(Builder.getExprRange(S),
1289 case syntax::NodeKind::PostfixUnaryOperatorExpression:
1290 Builder.markChildToken(S->getOperatorLoc(),
1293 Builder.foldNode(Builder.getExprRange(S),
1297 case syntax::NodeKind::CallExpression: {
1300 const auto *LParenToken =
1301 std::next(Builder.findToken(S->getArg(0)->getEndLoc()));
1304 if (LParenToken->kind() == clang::tok::l_paren)
1308 S->arg_begin() + 1, S->arg_end())),
1313 Builder.foldNode(Builder.getRange(S->getSourceRange()),
1314 new (allocator()) syntax::CallExpression, S);
1317 case syntax::NodeKind::UnknownExpression:
1318 return WalkUpFromExpr(S);
1320 llvm_unreachable(
"getOperatorNodeKind() does not return this value");
1327 auto Tokens = Builder.getDeclarationRange(S);
1328 if (Tokens.front().kind() == tok::coloncolon) {
1342 if (!WalkUpFromParenTypeLoc(L))
1367 for (
auto *
P : Params) {
1369 const auto *DelimiterToken = std::next(Builder.findToken(
P->getEndLoc()));
1370 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1374 if (!Params.empty())
1375 Builder.foldNode(Builder.getRange(Params.front()->getBeginLoc(),
1376 Params.back()->getEndLoc()),
1384 Builder.markChild(buildParameterDeclarationList(L.
getParams()),
1395 return WalkUpFromFunctionTypeLoc(L);
1397 auto *TrailingReturnTokens = buildTrailingReturn(L);
1400 return WalkUpFromFunctionTypeLoc(L);
1408 if (!WalkUpFromMemberPointerTypeLoc(L))
1415 Builder.foldNode(Builder.getRange(SR),
1423 bool WalkUpFromDeclStmt(
DeclStmt *S) {
1424 Builder.foldNode(Builder.getStmtRange(S),
1429 bool WalkUpFromNullStmt(
NullStmt *S) {
1430 Builder.foldNode(Builder.getStmtRange(S),
1436 Builder.markChildToken(S->getSwitchLoc(),
1439 Builder.foldNode(Builder.getStmtRange(S),
1444 bool WalkUpFromCaseStmt(
CaseStmt *S) {
1445 Builder.markChildToken(S->getKeywordLoc(),
1449 Builder.foldNode(Builder.getStmtRange(S),
1455 Builder.markChildToken(S->getKeywordLoc(),
1458 Builder.foldNode(Builder.getStmtRange(S),
1463 bool WalkUpFromIfStmt(
IfStmt *S) {
1465 Stmt *ConditionStatement = S->getCond();
1466 if (S->hasVarStorage())
1467 ConditionStatement = S->getConditionVariableDeclStmt();
1472 Builder.foldNode(Builder.getStmtRange(S),
1477 bool WalkUpFromForStmt(
ForStmt *S) {
1480 Builder.foldNode(Builder.getStmtRange(S),
1485 bool WalkUpFromWhileStmt(
WhileStmt *S) {
1486 Builder.markChildToken(S->getWhileLoc(),
1489 Builder.foldNode(Builder.getStmtRange(S),
1495 Builder.markChildToken(S->getContinueLoc(),
1497 Builder.foldNode(Builder.getStmtRange(S),
1502 bool WalkUpFromBreakStmt(
BreakStmt *S) {
1503 Builder.markChildToken(S->getBreakLoc(),
1505 Builder.foldNode(Builder.getStmtRange(S),
1511 Builder.markChildToken(S->getReturnLoc(),
1514 Builder.foldNode(Builder.getStmtRange(S),
1522 Builder.foldNode(Builder.getStmtRange(S),
1527 bool WalkUpFromEmptyDecl(
EmptyDecl *S) {
1528 Builder.foldNode(Builder.getDeclarationRange(S),
1536 Builder.foldNode(Builder.getDeclarationRange(S),
1542 Builder.foldNode(Builder.getDeclarationRange(S),
1549 Builder.foldNode(Builder.getDeclarationRange(S),
1555 Builder.foldNode(Builder.getDeclarationRange(S),
1560 bool WalkUpFromUsingDecl(
UsingDecl *S) {
1561 Builder.foldNode(Builder.getDeclarationRange(S),
1567 Builder.foldNode(Builder.getDeclarationRange(S),
1573 Builder.foldNode(Builder.getDeclarationRange(S),
1579 Builder.foldNode(Builder.getDeclarationRange(S),
1587 template <
class T>
bool processDeclaratorAndDeclaration(
T *D) {
1589 Builder.sourceManager(), D->getTypeSourceInfo()->getTypeLoc(),
1597 Builder.foldNode(Builder.getDeclarationRange(D),
1603 Builder.foldNode(Builder.getRange(Range), N,
nullptr);
1606 if (!Builder.isResponsibleForCreatingDeclaration(D)) {
1609 const auto *DelimiterToken = std::next(Builder.findToken(
Range.
getEnd()));
1610 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1614 auto DeclarationRange = Builder.getDeclarationRange(D);
1615 Builder.foldList(DeclarationRange, DL,
nullptr);
1618 Builder.foldNode(DeclarationRange,
1630 auto ReturnDeclaratorRange =
SourceRange(GetStartLoc().Visit(ReturnedType),
1631 ReturnedType.getEndLoc());
1633 if (ReturnDeclaratorRange.isValid()) {
1635 Builder.foldNode(Builder.getRange(ReturnDeclaratorRange),
1636 ReturnDeclarator,
nullptr);
1640 auto Return = Builder.getRange(ReturnedType.getSourceRange());
1641 const auto *Arrow = Return.begin() - 1;
1642 assert(Arrow->kind() == tok::arrow);
1645 if (ReturnDeclarator)
1648 Builder.foldNode(Tokens, R, L);
1652 void foldExplicitTemplateInstantiation(
1656 assert(!ExternKW || ExternKW->
kind() == tok::kw_extern);
1657 assert(TemplateKW && TemplateKW->
kind() == tok::kw_template);
1668 assert(TemplateKW && TemplateKW->
kind() == tok::kw_template);
1672 Builder.foldNode(Range, N, From);
1678 llvm::BumpPtrAllocator &allocator() {
return Builder.allocator(); }
1686 DeclsWithoutSemicolons.insert(D);
1692 Pending.assignRole(*findToken(
Loc), Role);
1698 Pending.assignRole(*
T, R);
1707 auto *SN = Mapping.find(N);
1708 assert(SN !=
nullptr);
1712 auto *SN = Mapping.find(NNSLoc);
1713 assert(SN !=
nullptr);
1722 if (
Expr *ChildExpr = dyn_cast<Expr>(Child)) {
1725 markExprChild(ChildExpr, NodeRole::Expression);
1728 Pending.foldChildren(TBTM.tokenBuffer(), getStmtRange(Child), ChildNode);
1730 ChildNode = Mapping.find(Child);
1732 assert(ChildNode !=
nullptr);
1733 setRole(ChildNode, Role);
1742 assert(ChildNode !=
nullptr);
1743 setRole(ChildNode, Role);
1749 auto It = LocationToToken.find(L);
1750 assert(It != LocationToToken.end());
1758 BuildTreeVisitor(Context, Builder).TraverseAST(Context);
1759 return std::move(Builder).finalize();
Forward declaration of all AST node types.
BoundNodesTreeBuilder Nodes
static Expr * IgnoreImplicit(Expr *E)
static SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T, SourceLocation Name, SourceRange Initializer)
Gets the range of declarator as defined by the C++ grammar.
static Expr * IgnoreCXXFunctionalCastExprWrappingConstructor(Expr *E)
static Expr * IgnoreImplicitConstructorSingleStep(Expr *E)
static CallExpr::arg_range dropDefaultArgs(CallExpr::arg_range Args)
static LLVM_ATTRIBUTE_UNUSED bool isImplicitExpr(Expr *E)
static syntax::NodeKind getOperatorNodeKind(const CXXOperatorCallExpr &E)
static SourceLocation getQualifiedNameStart(NamedDecl *D)
Get the start of the qualified name.
static SourceRange getInitializerRange(Decl *D)
Gets the range of the initializer inside an init-declarator C++ [dcl.decl].
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines various enumerations that describe declaration and type specifiers.
Defines the clang::TokenKind enum and support functions.
Defines the clang::TypeLoc interface and its subclasses.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
const LangOptions & getLangOpts() const
const TargetInfo & getTargetInfo() const
DiagnosticsEngine & getDiagnostics() const
Wrapper for source info for arrays.
SourceLocation getLBracketLoc() const
Expr * getSizeExpr() const
SourceLocation getRBracketLoc() const
A builtin binary operation expression such as "x + y" or "x <= y".
Wrapper for source info for block pointers.
BreakStmt - This represents a break.
A boolean literal, per ([C++ lex.bool] Boolean literals).
Represents a call to a C++ constructor.
A default argument (C++ [dcl.fct.default]).
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
The null pointer literal (C++11 [lex.nullptr])
A call to an overloaded operator written using operator syntax.
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
Represents the this expression in C++.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
llvm::iterator_range< arg_iterator > arg_range
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
CaseStmt - Represent a case statement.
Represents a class template specialization, which refers to a class template with a given set of temp...
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
CompoundStmt - This represents a group of statements like { stmt stmt }.
ContinueStmt - This represents a continue.
A reference to a declared variable, function, enum, etc.
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Decl * getNextDeclInContext()
SourceLocation getLocation() const
SourceLocation getBeginLoc() const LLVM_READONLY
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
bool isIdentifier() const
Predicate functions for querying what type of name this is.
Represents a ValueDecl that came out of a declarator.
A qualified reference to a name whose declaration cannot yet be resolved.
Represents an empty-declaration.
This represents one expression.
ForStmt - This represents a 'for (init;cond;inc)' stmt.
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Wrapper for source info for functions.
ArrayRef< ParmVarDecl * > getParams() const
TypeLoc getReturnLoc() const
SourceLocation getLParenLoc() const
SourceLocation getRParenLoc() const
IfStmt - This represents an if/then/else.
const TypeClass * getTypePtr() const
Represents a linkage specification.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Wrapper for source info for member pointers.
SourceRange getLocalSourceRange() const
This represents a decl that may have a name.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Represents a C++ namespace alias.
Represent a C++ namespace.
A C++ nested-name-specifier augmented with source location information.
SourceLocation getBeginLoc() const
Retrieve the location of the beginning of this nested-name-specifier.
TypeLoc getTypeLoc() const
For a nested-name-specifier that refers to a type, retrieve the type with source-location information...
SourceRange getLocalSourceRange() const
Retrieve the source range covering just the last part of this nested-name-specifier,...
NestedNameSpecifier * getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
bool hasQualifier() const
Evaluates true when this nested-name-specifier location is non-empty.
SourceRange getSourceRange() const LLVM_READONLY
Retrieve the source range covering the entirety of this nested-name-specifier.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
SpecifierKind getKind() const
Determine what kind of nested name specifier is stored.
const Type * getAsType() const
Retrieve the type stored in this nested name specifier.
@ NamespaceAlias
A namespace alias, stored as a NamespaceAliasDecl*.
@ TypeSpec
A type, stored as a Type*.
@ TypeSpecWithTemplate
A type that was preceded by the 'template' keyword, stored as a Type*.
@ Identifier
An identifier, stored as an IdentifierInfo*.
@ Global
The global specifier '::'. There is no stored value.
@ Namespace
A namespace, stored as a NamespaceDecl*.
NullStmt - This is the null statement ";": C99 6.8.3p3.
NumericLiteralParser - This performs strict semantic analysis of the content of a ppnumber,...
Wraps an ObjCPointerType with source location information.
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
ParenExpr - This represents a parethesized expression, e.g.
SourceLocation getRParenLoc() const
SourceLocation getLParenLoc() const
TypeLoc getInnerLoc() const
TypeLoc getPointeeLoc() const
Wrapper for source info for pointers.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
void setBegin(SourceLocation b)
SourceLocation getEnd() const
SourceLocation getBegin() const
Represents a C++11 static_assert declaration.
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
StringLiteral - This represents a string literal expression, e.g.
SwitchStmt - This represents a 'switch' stmt.
Represents the declaration of a struct/union/class/enum.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Stores a list of template parameters for a TemplateDecl and its derived classes.
The top declaration context.
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Base wrapper for a particular "section" of type source info.
T castAs() const
Convert to the specified TypeLoc type, asserting that this TypeLoc is of the desired type.
SourceLocation getEndLoc() const
Get the end source location.
Base class for declarations which introduce a typedef-name.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Represents a dependent using declaration which was marked with typename.
Represents a dependent using declaration which was not marked with typename.
A call to a literal operator (C++11 [over.literal]) written as a user-defined literal (C++11 [lit....
@ LOK_String
operator "" X (const CharT *, size_t)
@ LOK_Raw
Raw form: operator "" X (const char *)
@ LOK_Floating
operator "" X (long double)
@ LOK_Integer
operator "" X (unsigned long long)
@ LOK_Template
Raw form: operator "" X<cs...> ()
@ LOK_Character
operator "" X (CharT)
Represents a C++ using-declaration.
Represents C++ using-directive.
WhileStmt - This represents a 'while' stmt.
A memory arena for syntax trees.
llvm::BumpPtrAllocator & getAllocator()
Array size specified inside a declarator.
Models arguments of a function call.
{ statement1; statement2; … }
A declaration that can appear at the top-level.
A semicolon in the top-level context. Does not declare anything.
The no-op statement, i.e. ';'.
template <declaration> Examples: template struct X<int> template void foo<int>() template int var<dou...
Expression in a statement position, e.g.
for (<init>; <cond>; <increment>) <body>
if (cond) <then-statement> else <else-statement> FIXME: add condition that models 'expression or vari...
A leaf node points to a single token.
extern <string-literal> declaration extern <string-literal> { <decls> }
A list of Elements separated or terminated by a fixed token.
Member pointer inside a declarator E.g.
namespace <name> = <namespace-reference>
namespace <name> { <decls> }
Models a nested-name-specifier.
Models a parameter-declaration-list which appears within parameters-and-qualifiers.
Parameter list for a function type and a trailing return type, if the function has one.
Declarator inside parentheses.
for (<decl> : <init>) <body>
Groups multiple declarators (e.g.
A top-level declarator without parentheses.
static_assert(<condition>, <message>) static_assert(<condition>)
template <template-parameters> <declaration>
A TokenBuffer-powered token manager.
const TokenBuffer & tokenBuffer() const
SourceManager & sourceManager()
A list of tokens obtained by preprocessing a text buffer and operations to map between the expanded a...
std::optional< llvm::ArrayRef< syntax::Token > > spelledForExpanded(llvm::ArrayRef< syntax::Token > Expanded) const
Returns the subrange of spelled tokens corresponding to AST node spanning Expanded.
llvm::ArrayRef< syntax::Token > expandedTokens() const
All tokens produced by the preprocessor after all macro replacements, directives, etc.
uintptr_t Key
A key to identify a specific token.
A token coming directly from a file or from a macro invocation.
tok::TokenKind kind() const
Trailing return type after the parameter list, including the arrow token.
A node that has children and represents a syntactic language construct.
Declaration of an unknown kind, e.g. not yet supported in syntax trees.
An expression of an unknown kind, i.e.
A statement of an unknown kind, i.e.
Models an unqualified-id.
using <scope>::<name> using typename <scope>::<name>
A helper class for constructing the syntax tree while traversing a clang AST.
void foldList(ArrayRef< syntax::Token > SuperRange, syntax::List *New, ASTPtr From)
Populate children for New list, assuming it covers tokens from a subrange of SuperRange.
void markExprChild(Expr *Child, NodeRole Role)
Should be called for expressions in non-statement position to avoid wrapping into expression statemen...
ArrayRef< syntax::Token > getExprRange(const Expr *E) const
void markChildToken(SourceLocation Loc, NodeRole R)
Set role for a token starting at Loc.
void markChild(syntax::Node *N, NodeRole R)
Set role for N.
void foldNode(ArrayRef< syntax::Token > Range, syntax::Tree *New, TypeLoc L)
ArrayRef< syntax::Token > getRange(SourceLocation First, SourceLocation Last) const
Finds the syntax tokens corresponding to the passed source locations.
const syntax::Token * findToken(SourceLocation L) const
Finds a token starting at L. The token must exist if L is valid.
ArrayRef< syntax::Token > getRange(SourceRange Range) const
Finds the syntax tokens corresponding to the SourceRange.
const SourceManager & sourceManager() const
void noticeDeclWithoutSemicolon(Decl *D)
Notifies that we should not consume trailing semicolon when computing token range of D.
ArrayRef< syntax::Token > getDeclarationRange(Decl *D)
bool isResponsibleForCreatingDeclaration(const Decl *D) const
Returns true if D is the last declarator in a chain and is thus reponsible for creating SimpleDeclara...
syntax::TranslationUnit * finalize() &&
Finish building the tree and consume the root node.
ArrayRef< syntax::Token > getStmtRange(const Stmt *S) const
Find the adjusted range for the statement, consuming the trailing semicolon when needed.
ArrayRef< syntax::Token > getTemplateRange(const ClassTemplateSpecializationDecl *D) const
llvm::BumpPtrAllocator & allocator()
void foldNode(ArrayRef< syntax::Token > Range, syntax::Tree *New, ASTPtr From)
Populate children for New node, assuming it covers tokens from Range.
TreeBuilder(syntax::Arena &Arena, TokenBufferTokenManager &TBTM)
void foldNode(llvm::ArrayRef< syntax::Token > Range, syntax::Tree *New, NestedNameSpecifierLoc From)
void markStmtChild(Stmt *Child, NodeRole Role)
Mark the Child node with a corresponding Role.
uint32_t Literal
Literals are represented as positive integers.
NodeRole
A relation between a parent and child node, e.g.
@ ListElement
List API roles.
@ LiteralToken
A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
@ Detached
A node without a parent.
@ CloseParen
A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.
@ IntroducerKeyword
A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.
@ Unknown
Children of an unknown semantic nature, e.g. skipped tokens, comments.
@ BodyStatement
An inner statement for those that have only a single child of kind statement, e.g.
@ OpenParen
An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.
@ ArrowToken
Tokens or Keywords.
syntax::TranslationUnit * buildSyntaxTree(Arena &A, TokenBufferTokenManager &TBTM, ASTContext &Context)
Build a syntax tree for the main file.
NodeKind
A kind of a syntax node, used for implementing casts.
The JSON file list parser is used to communicate input to InstallAPI.
@ OO_None
Not an overloaded operator.
@ NUM_OVERLOADED_OPERATORS
Expr * IgnoreExprNodes(Expr *E, FnTys &&... Fns)
Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *, Recursively apply each of the f...
const FunctionProtoType * T
Expr * IgnoreImplicitSingleStep(Expr *E)