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"
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();
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();
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);
165 switch (
E.getOperator()) {
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;
204 switch (
E.getNumArgs()) {
206 return syntax::NodeKind::PrefixUnaryOperatorExpression;
208 return syntax::NodeKind::PostfixUnaryOperatorExpression;
210 llvm_unreachable(
"Invalid number of arguments for operator");
217 switch (
E.getNumArgs()) {
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.");
254 auto DN =
D->getDeclName();
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()) {
302 if (End.isInvalid() ||
SM.isBeforeInTranslationUnit(End, Name))
307 assert(
SM.isBeforeInTranslationUnit(End, InitializerEnd) ||
308 End == InitializerEnd);
309 End = InitializerEnd;
316using ASTPtr = llvm::PointerUnion<Stmt *, Decl *>;
320class 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});
388 Mapping.add(From, New);
401 Mapping.add(From, New);
409 auto ListRange = Pending.shrinkToFitList(SuperRange);
410 Pending.foldChildren(TBTM.
tokenBuffer(), ListRange, New);
412 Mapping.add(From, New);
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());
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;
699 llvm::DenseSet<Decl *> DeclsWithoutSemicolons;
700 ASTToSyntaxMapping Mapping;
707 : Builder(Builder), Context(Context) {}
712 return processDeclaratorAndDeclaration(DD);
716 return processDeclaratorAndDeclaration(TD);
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),
805 Builder.foldNode(Builder.getStmtRange(S),
810 bool TraverseIfStmt(
IfStmt *S) {
811 bool Result = [&,
this]() {
818 if (S->hasVarStorage()) {
839 bool Result = [&,
this]() {
842 if (S->getLoopVariable() && !
TraverseDecl(S->getLoopVariable()))
844 if (S->getRangeInit() && !
TraverseStmt(S->getRangeInit()))
850 WalkUpFromCXXForRangeStmt(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) {
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.");
922 Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
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(),
992 syntax::NodeRole::Unknown);
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);
1035 Builder.markChild(NS, syntax::NodeRole::ListElement);
1036 Builder.markChildToken(It.getEndLoc(), syntax::NodeRole::ListDelimiter);
1038 Builder.foldNode(Builder.getRange(QualifierLoc.
getSourceRange()),
1049 Builder.markChild(QualifierLoc, syntax::NodeRole::Qualifier);
1050 if (TemplateKeywordLoc.
isValid())
1051 Builder.markChildToken(TemplateKeywordLoc,
1052 syntax::NodeRole::TemplateKeyword);
1056 Builder.foldNode(Builder.getRange(UnqualifiedIdLoc), TheUnqualifiedId,
1058 Builder.markChild(TheUnqualifiedId, syntax::NodeRole::UnqualifiedId);
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);
1085 Builder.markChild(TheIdExpression, syntax::NodeRole::Member);
1087 Builder.markExprChild(S->getBase(), syntax::NodeRole::Object);
1088 Builder.markChildToken(S->getOperatorLoc(), syntax::NodeRole::AccessToken);
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(),
1113 syntax::NodeRole::IntroducerKeyword);
1114 Builder.foldNode(Builder.getExprRange(S),
1115 new (allocator()) syntax::ThisExpression, S);
1120 bool WalkUpFromParenExpr(
ParenExpr *S) {
1121 Builder.markChildToken(S->getLParen(), syntax::NodeRole::OpenParen);
1122 Builder.markExprChild(S->getSubExpr(), syntax::NodeRole::SubExpression);
1123 Builder.markChildToken(S->getRParen(), syntax::NodeRole::CloseParen);
1124 Builder.foldNode(Builder.getExprRange(S),
1125 new (allocator()) syntax::ParenExpression, S);
1130 Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1131 Builder.foldNode(Builder.getExprRange(S),
1132 new (allocator()) syntax::IntegerLiteralExpression, S);
1137 Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1138 Builder.foldNode(Builder.getExprRange(S),
1139 new (allocator()) syntax::CharacterLiteralExpression, S);
1144 Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1145 Builder.foldNode(Builder.getExprRange(S),
1146 new (allocator()) syntax::FloatingLiteralExpression, S);
1151 Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
1152 Builder.foldNode(Builder.getExprRange(S),
1153 new (allocator()) syntax::StringLiteralExpression, S);
1158 Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1159 Builder.foldNode(Builder.getExprRange(S),
1160 new (allocator()) syntax::BoolLiteralExpression, S);
1165 Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1166 Builder.foldNode(Builder.getExprRange(S),
1167 new (allocator()) syntax::CxxNullPtrExpression, S);
1172 Builder.markChildToken(S->getOperatorLoc(),
1173 syntax::NodeRole::OperatorToken);
1174 Builder.markExprChild(S->getSubExpr(), syntax::NodeRole::Operand);
1177 Builder.foldNode(Builder.getExprRange(S),
1181 Builder.foldNode(Builder.getExprRange(S),
1189 Builder.markExprChild(S->getLHS(), syntax::NodeRole::LeftHandSide);
1190 Builder.markChildToken(S->getOperatorLoc(),
1191 syntax::NodeRole::OperatorToken);
1192 Builder.markExprChild(S->getRHS(), syntax::NodeRole::RightHandSide);
1193 Builder.foldNode(Builder.getExprRange(S),
1203 for (
auto *Arg : Args) {
1204 Builder.markExprChild(Arg, syntax::NodeRole::ListElement);
1205 const auto *DelimiterToken =
1206 std::next(Builder.findToken(Arg->getEndLoc()));
1207 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1208 Builder.markChildToken(DelimiterToken, syntax::NodeRole::ListDelimiter);
1213 Builder.foldNode(Builder.getRange((*Args.begin())->getBeginLoc(),
1214 (*(Args.end() - 1))->getEndLoc()),
1220 bool WalkUpFromCallExpr(
CallExpr *S) {
1221 Builder.markExprChild(S->getCallee(), syntax::NodeRole::Callee);
1223 const auto *LParenToken =
1224 std::next(Builder.findToken(S->getCallee()->getEndLoc()));
1227 if (LParenToken->kind() == clang::tok::l_paren)
1228 Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen);
1230 Builder.markChild(buildCallArguments(S->arguments()),
1231 syntax::NodeRole::Arguments);
1233 Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen);
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);
1268 return WalkUpFromCXXOperatorCallExpr(S);
1273 case syntax::NodeKind::BinaryOperatorExpression:
1274 Builder.markExprChild(S->getArg(0), syntax::NodeRole::LeftHandSide);
1275 Builder.markChildToken(S->getOperatorLoc(),
1276 syntax::NodeRole::OperatorToken);
1277 Builder.markExprChild(S->getArg(1), syntax::NodeRole::RightHandSide);
1278 Builder.foldNode(Builder.getExprRange(S),
1281 case syntax::NodeKind::PrefixUnaryOperatorExpression:
1282 Builder.markChildToken(S->getOperatorLoc(),
1283 syntax::NodeRole::OperatorToken);
1284 Builder.markExprChild(S->getArg(0), syntax::NodeRole::Operand);
1285 Builder.foldNode(Builder.getExprRange(S),
1289 case syntax::NodeKind::PostfixUnaryOperatorExpression:
1290 Builder.markChildToken(S->getOperatorLoc(),
1291 syntax::NodeRole::OperatorToken);
1292 Builder.markExprChild(S->getArg(0), syntax::NodeRole::Operand);
1293 Builder.foldNode(Builder.getExprRange(S),
1297 case syntax::NodeKind::CallExpression: {
1298 Builder.markExprChild(S->getArg(0), syntax::NodeRole::Callee);
1300 const auto *LParenToken =
1301 std::next(Builder.findToken(S->getArg(0)->getEndLoc()));
1304 if (LParenToken->kind() == clang::tok::l_paren)
1305 Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen);
1308 S->arg_begin() + 1, S->arg_end())),
1309 syntax::NodeRole::Arguments);
1311 Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen);
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))
1348 Builder.markChildToken(L.
getLParenLoc(), syntax::NodeRole::OpenParen);
1349 Builder.markChildToken(L.
getRParenLoc(), syntax::NodeRole::CloseParen);
1357 Builder.markChildToken(L.
getLBracketLoc(), syntax::NodeRole::OpenParen);
1358 Builder.markExprChild(L.
getSizeExpr(), syntax::NodeRole::Size);
1359 Builder.markChildToken(L.
getRBracketLoc(), syntax::NodeRole::CloseParen);
1367 for (
auto *
P : Params) {
1368 Builder.markChild(
P, syntax::NodeRole::ListElement);
1369 const auto *DelimiterToken = std::next(Builder.findToken(
P->getEndLoc()));
1370 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1371 Builder.markChildToken(DelimiterToken, syntax::NodeRole::ListDelimiter);
1374 if (!Params.empty())
1375 Builder.foldNode(Builder.getRange(Params.front()->getBeginLoc(),
1376 Params.back()->getEndLoc()),
1382 Builder.markChildToken(L.
getLParenLoc(), syntax::NodeRole::OpenParen);
1384 Builder.markChild(buildParameterDeclarationList(L.
getParams()),
1385 syntax::NodeRole::Parameters);
1387 Builder.markChildToken(L.
getRParenLoc(), syntax::NodeRole::CloseParen);
1395 return WalkUpFromFunctionTypeLoc(L);
1397 auto *TrailingReturnTokens = buildTrailingReturn(L);
1399 Builder.markChild(TrailingReturnTokens, syntax::NodeRole::TrailingReturn);
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(),
1437 syntax::NodeRole::IntroducerKeyword);
1438 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1439 Builder.foldNode(Builder.getStmtRange(S),
1444 bool WalkUpFromCaseStmt(
CaseStmt *S) {
1445 Builder.markChildToken(S->getKeywordLoc(),
1446 syntax::NodeRole::IntroducerKeyword);
1447 Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseValue);
1448 Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1449 Builder.foldNode(Builder.getStmtRange(S),
1455 Builder.markChildToken(S->getKeywordLoc(),
1456 syntax::NodeRole::IntroducerKeyword);
1457 Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1458 Builder.foldNode(Builder.getStmtRange(S),
1463 bool WalkUpFromIfStmt(
IfStmt *S) {
1464 Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword);
1465 Stmt *ConditionStatement = S->getCond();
1466 if (S->hasVarStorage())
1467 ConditionStatement = S->getConditionVariableDeclStmt();
1468 Builder.markStmtChild(ConditionStatement, syntax::NodeRole::Condition);
1469 Builder.markStmtChild(S->getThen(), syntax::NodeRole::ThenStatement);
1470 Builder.markChildToken(S->getElseLoc(), syntax::NodeRole::ElseKeyword);
1471 Builder.markStmtChild(S->getElse(), syntax::NodeRole::ElseStatement);
1472 Builder.foldNode(Builder.getStmtRange(S),
1477 bool WalkUpFromForStmt(
ForStmt *S) {
1478 Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1479 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1480 Builder.foldNode(Builder.getStmtRange(S),
1485 bool WalkUpFromWhileStmt(
WhileStmt *S) {
1486 Builder.markChildToken(S->getWhileLoc(),
1487 syntax::NodeRole::IntroducerKeyword);
1488 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1489 Builder.foldNode(Builder.getStmtRange(S),
1495 Builder.markChildToken(S->getContinueLoc(),
1496 syntax::NodeRole::IntroducerKeyword);
1497 Builder.foldNode(Builder.getStmtRange(S),
1502 bool WalkUpFromBreakStmt(
BreakStmt *S) {
1503 Builder.markChildToken(S->getBreakLoc(),
1504 syntax::NodeRole::IntroducerKeyword);
1505 Builder.foldNode(Builder.getStmtRange(S),
1511 Builder.markChildToken(S->getReturnLoc(),
1512 syntax::NodeRole::IntroducerKeyword);
1513 Builder.markExprChild(S->getRetValue(), syntax::NodeRole::ReturnValue);
1514 Builder.foldNode(Builder.getStmtRange(S),
1520 Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1521 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1522 Builder.foldNode(Builder.getStmtRange(S),
1527 bool WalkUpFromEmptyDecl(
EmptyDecl *S) {
1528 Builder.foldNode(Builder.getDeclarationRange(S),
1534 Builder.markExprChild(S->getAssertExpr(), syntax::NodeRole::Condition);
1535 Builder.markExprChild(S->getMessage(), syntax::NodeRole::Message);
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(),
1596 syntax::NodeRole::Declarators);
1597 Builder.foldNode(Builder.getDeclarationRange(
D),
1603 Builder.foldNode(Builder.getRange(
Range), N,
nullptr);
1604 Builder.markChild(N, syntax::NodeRole::ListElement);
1606 if (!Builder.isResponsibleForCreatingDeclaration(
D)) {
1609 const auto *DelimiterToken = std::next(Builder.findToken(
Range.
getEnd()));
1610 if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1611 Builder.markChildToken(DelimiterToken, syntax::NodeRole::ListDelimiter);
1614 auto DeclarationRange = Builder.getDeclarationRange(
D);
1615 Builder.foldList(DeclarationRange, DL,
nullptr);
1617 Builder.markChild(DL, syntax::NodeRole::Declarators);
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);
1644 Builder.markChildToken(Arrow, syntax::NodeRole::ArrowToken);
1645 if (ReturnDeclarator)
1646 Builder.markChild(ReturnDeclarator, syntax::NodeRole::Declarator);
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);
1658 Builder.markChildToken(ExternKW, syntax::NodeRole::ExternKeyword);
1659 Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1660 Builder.markChild(InnerDeclaration, syntax::NodeRole::Declaration);
1668 assert(TemplateKW && TemplateKW->
kind() == tok::kw_template);
1669 Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1672 Builder.foldNode(
Range, N, From);
1673 Builder.markChild(N, syntax::NodeRole::Declaration);
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)) {
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 SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T, SourceLocation Name, SourceRange Initializer)
Gets the range of declarator as defined by the C++ grammar.
static Expr * IgnoreImplicit(Expr *E)
static CallExpr::arg_range dropDefaultArgs(CallExpr::arg_range Args)
static LLVM_ATTRIBUTE_UNUSED bool isImplicitExpr(Expr *E)
static Expr * IgnoreCXXFunctionalCastExprWrappingConstructor(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].
static Expr * IgnoreImplicitConstructorSingleStep(Expr *E)
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
DiagnosticsEngine & getDiagnostics() const
const TargetInfo & getTargetInfo() 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.
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
CaseStmt - Represent a case statement.
Represents a class template specialization, which refers to a class template with a given set of temp...
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.
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.
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,...
bool hasQualifier() const
Evaluates true when this nested-name-specifier location is non-empty.
NestedNameSpecifier * getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
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.
@ 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*.
const Type * getAsType() const
Retrieve the type stored in this nested name specifier.
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 parenthesized 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 TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Recursively visit a C++ nested-name-specifier with location information.
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
bool WalkUpFromStmt(Stmt *S)
bool TraverseTypeLoc(TypeLoc TL)
Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument ty...
bool shouldTraversePostOrder() const
Return whether this visitor should traverse post-order.
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.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
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.
RetTy Visit(TypeLoc TyLoc)
RetTy VisitTypeLoc(TypeLoc TyLoc)
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...
llvm::ArrayRef< syntax::Token > expandedTokens() const
All tokens produced by the preprocessor after all macro replacements, directives, etc.
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.
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.
ArrayRef< syntax::Token > getRange(SourceLocation First, SourceLocation Last) const
Finds the syntax tokens corresponding to the passed source locations.
ArrayRef< syntax::Token > getStmtRange(const Stmt *S) const
Find the adjusted range for the statement, consuming the trailing semicolon when needed.
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...
const SourceManager & sourceManager() const
void markChildToken(SourceLocation Loc, NodeRole R)
Set role for a token starting at Loc.
ArrayRef< syntax::Token > getDeclarationRange(Decl *D)
void markChild(syntax::Node *N, NodeRole R)
Set role for N.
void foldNode(ArrayRef< syntax::Token > Range, syntax::Tree *New, TypeLoc L)
const syntax::Token * findToken(SourceLocation L) const
Finds a token starting at L. The token must exist if L is valid.
void noticeDeclWithoutSemicolon(Decl *D)
Notifies that we should not consume trailing semicolon when computing token range of D.
ArrayRef< syntax::Token > getRange(SourceRange Range) const
Finds the syntax tokens corresponding to the SourceRange.
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.
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)
ArrayRef< syntax::Token > getTemplateRange(const ClassTemplateSpecializationDecl *D) const
void foldNode(llvm::ArrayRef< syntax::Token > Range, syntax::Tree *New, NestedNameSpecifierLoc From)
ArrayRef< syntax::Token > getExprRange(const Expr *E) const
void markStmtChild(Stmt *Child, NodeRole Role)
Mark the Child node with a corresponding Role.
llvm::BumpPtrAllocator & allocator()
uint32_t Literal
Literals are represented as positive integers.
NodeRole
A relation between a parent and child node, e.g.
@ ListElement
List API roles.
@ Detached
A node without a parent.
@ Unknown
Children of an unknown semantic nature, e.g. skipped tokens, comments.
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...
@ Result
The result type of a method or function.
Expr * IgnoreImplicitSingleStep(Expr *E)
const FunctionProtoType * T
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)