TokenAnnotator.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief This file implements a token annotator, i.e. creates
  12. /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "TokenAnnotator.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/Support/Debug.h"
  19. #define DEBUG_TYPE "format-token-annotator"
  20. namespace clang {
  21. namespace format {
  22. namespace {
  23. /// \brief A parser that gathers additional information about tokens.
  24. ///
  25. /// The \c TokenAnnotator tries to match parenthesis and square brakets and
  26. /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
  27. /// into template parameter lists.
  28. class AnnotatingParser {
  29. public:
  30. AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
  31. const AdditionalKeywords &Keywords)
  32. : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
  33. Keywords(Keywords) {
  34. Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
  35. resetTokenMetadata(CurrentToken);
  36. }
  37. private:
  38. bool parseAngle() {
  39. if (!CurrentToken)
  40. return false;
  41. FormatToken *Left = CurrentToken->Previous;
  42. Left->ParentBracket = Contexts.back().ContextKind;
  43. ScopedContextCreator ContextCreator(*this, tok::less, 10);
  44. // If this angle is in the context of an expression, we need to be more
  45. // hesitant to detect it as opening template parameters.
  46. bool InExprContext = Contexts.back().IsExpression;
  47. Contexts.back().IsExpression = false;
  48. // If there's a template keyword before the opening angle bracket, this is a
  49. // template parameter, not an argument.
  50. Contexts.back().InTemplateArgument =
  51. Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
  52. if (Style.Language == FormatStyle::LK_Java &&
  53. CurrentToken->is(tok::question))
  54. next();
  55. while (CurrentToken) {
  56. if (CurrentToken->is(tok::greater)) {
  57. Left->MatchingParen = CurrentToken;
  58. CurrentToken->MatchingParen = Left;
  59. CurrentToken->Type = TT_TemplateCloser;
  60. next();
  61. return true;
  62. }
  63. if (CurrentToken->is(tok::question) &&
  64. Style.Language == FormatStyle::LK_Java) {
  65. next();
  66. continue;
  67. }
  68. if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
  69. (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext))
  70. return false;
  71. // If a && or || is found and interpreted as a binary operator, this set
  72. // of angles is likely part of something like "a < b && c > d". If the
  73. // angles are inside an expression, the ||/&& might also be a binary
  74. // operator that was misinterpreted because we are parsing template
  75. // parameters.
  76. // FIXME: This is getting out of hand, write a decent parser.
  77. if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
  78. CurrentToken->Previous->is(TT_BinaryOperator) &&
  79. Contexts[Contexts.size() - 2].IsExpression &&
  80. !Line.startsWith(tok::kw_template))
  81. return false;
  82. updateParameterCount(Left, CurrentToken);
  83. if (!consumeToken())
  84. return false;
  85. }
  86. return false;
  87. }
  88. bool parseParens(bool LookForDecls = false) {
  89. if (!CurrentToken)
  90. return false;
  91. FormatToken *Left = CurrentToken->Previous;
  92. Left->ParentBracket = Contexts.back().ContextKind;
  93. ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
  94. // FIXME: This is a bit of a hack. Do better.
  95. Contexts.back().ColonIsForRangeExpr =
  96. Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
  97. bool StartsObjCMethodExpr = false;
  98. if (CurrentToken->is(tok::caret)) {
  99. // (^ can start a block type.
  100. Left->Type = TT_ObjCBlockLParen;
  101. } else if (FormatToken *MaybeSel = Left->Previous) {
  102. // @selector( starts a selector.
  103. if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
  104. MaybeSel->Previous->is(tok::at)) {
  105. StartsObjCMethodExpr = true;
  106. }
  107. }
  108. if (Left->Previous &&
  109. (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
  110. tok::kw_while, tok::l_paren, tok::comma) ||
  111. Left->Previous->is(TT_BinaryOperator))) {
  112. // static_assert, if and while usually contain expressions.
  113. Contexts.back().IsExpression = true;
  114. } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
  115. Left->Previous->MatchingParen &&
  116. Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
  117. // This is a parameter list of a lambda expression.
  118. Contexts.back().IsExpression = false;
  119. } else if (Line.InPPDirective &&
  120. (!Left->Previous ||
  121. !Left->Previous->isOneOf(tok::identifier,
  122. TT_OverloadedOperator))) {
  123. Contexts.back().IsExpression = true;
  124. } else if (Contexts[Contexts.size() - 2].CaretFound) {
  125. // This is the parameter list of an ObjC block.
  126. Contexts.back().IsExpression = false;
  127. } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
  128. Left->Type = TT_AttributeParen;
  129. } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
  130. // The first argument to a foreach macro is a declaration.
  131. Contexts.back().IsForEachMacro = true;
  132. Contexts.back().IsExpression = false;
  133. } else if (Left->Previous && Left->Previous->MatchingParen &&
  134. Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
  135. Contexts.back().IsExpression = false;
  136. }
  137. if (StartsObjCMethodExpr) {
  138. Contexts.back().ColonIsObjCMethodExpr = true;
  139. Left->Type = TT_ObjCMethodExpr;
  140. }
  141. bool MightBeFunctionType = CurrentToken->is(tok::star);
  142. bool HasMultipleLines = false;
  143. bool HasMultipleParametersOnALine = false;
  144. bool MightBeObjCForRangeLoop =
  145. Left->Previous && Left->Previous->is(tok::kw_for);
  146. while (CurrentToken) {
  147. // LookForDecls is set when "if (" has been seen. Check for
  148. // 'identifier' '*' 'identifier' followed by not '=' -- this
  149. // '*' has to be a binary operator but determineStarAmpUsage() will
  150. // categorize it as an unary operator, so set the right type here.
  151. if (LookForDecls && CurrentToken->Next) {
  152. FormatToken *Prev = CurrentToken->getPreviousNonComment();
  153. if (Prev) {
  154. FormatToken *PrevPrev = Prev->getPreviousNonComment();
  155. FormatToken *Next = CurrentToken->Next;
  156. if (PrevPrev && PrevPrev->is(tok::identifier) &&
  157. Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
  158. CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
  159. Prev->Type = TT_BinaryOperator;
  160. LookForDecls = false;
  161. }
  162. }
  163. }
  164. if (CurrentToken->Previous->is(TT_PointerOrReference) &&
  165. CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
  166. tok::coloncolon))
  167. MightBeFunctionType = true;
  168. if (CurrentToken->Previous->is(TT_BinaryOperator))
  169. Contexts.back().IsExpression = true;
  170. if (CurrentToken->is(tok::r_paren)) {
  171. if (MightBeFunctionType && CurrentToken->Next &&
  172. (CurrentToken->Next->is(tok::l_paren) ||
  173. (CurrentToken->Next->is(tok::l_square) &&
  174. !Contexts.back().IsExpression)))
  175. Left->Type = TT_FunctionTypeLParen;
  176. Left->MatchingParen = CurrentToken;
  177. CurrentToken->MatchingParen = Left;
  178. if (StartsObjCMethodExpr) {
  179. CurrentToken->Type = TT_ObjCMethodExpr;
  180. if (Contexts.back().FirstObjCSelectorName) {
  181. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  182. Contexts.back().LongestObjCSelectorName;
  183. }
  184. }
  185. if (Left->is(TT_AttributeParen))
  186. CurrentToken->Type = TT_AttributeParen;
  187. if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
  188. CurrentToken->Type = TT_JavaAnnotation;
  189. if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
  190. CurrentToken->Type = TT_LeadingJavaAnnotation;
  191. if (!HasMultipleLines)
  192. Left->PackingKind = PPK_Inconclusive;
  193. else if (HasMultipleParametersOnALine)
  194. Left->PackingKind = PPK_BinPacked;
  195. else
  196. Left->PackingKind = PPK_OnePerLine;
  197. next();
  198. return true;
  199. }
  200. if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
  201. return false;
  202. if (CurrentToken->is(tok::l_brace))
  203. Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
  204. if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
  205. !CurrentToken->Next->HasUnescapedNewline &&
  206. !CurrentToken->Next->isTrailingComment())
  207. HasMultipleParametersOnALine = true;
  208. if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
  209. CurrentToken->isSimpleTypeSpecifier())
  210. Contexts.back().IsExpression = false;
  211. if (CurrentToken->isOneOf(tok::semi, tok::colon))
  212. MightBeObjCForRangeLoop = false;
  213. if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in))
  214. CurrentToken->Type = TT_ObjCForIn;
  215. // When we discover a 'new', we set CanBeExpression to 'false' in order to
  216. // parse the type correctly. Reset that after a comma.
  217. if (CurrentToken->is(tok::comma))
  218. Contexts.back().CanBeExpression = true;
  219. FormatToken *Tok = CurrentToken;
  220. if (!consumeToken())
  221. return false;
  222. updateParameterCount(Left, Tok);
  223. if (CurrentToken && CurrentToken->HasUnescapedNewline)
  224. HasMultipleLines = true;
  225. }
  226. return false;
  227. }
  228. bool parseSquare() {
  229. if (!CurrentToken)
  230. return false;
  231. // A '[' could be an index subscript (after an identifier or after
  232. // ')' or ']'), it could be the start of an Objective-C method
  233. // expression, or it could the start of an Objective-C array literal.
  234. FormatToken *Left = CurrentToken->Previous;
  235. Left->ParentBracket = Contexts.back().ContextKind;
  236. FormatToken *Parent = Left->getPreviousNonComment();
  237. bool StartsObjCMethodExpr =
  238. Style.Language == FormatStyle::LK_Cpp &&
  239. Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
  240. CurrentToken->isNot(tok::l_brace) &&
  241. (!Parent ||
  242. Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
  243. tok::kw_return, tok::kw_throw) ||
  244. Parent->isUnaryOperator() ||
  245. Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
  246. getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
  247. bool ColonFound = false;
  248. unsigned BindingIncrease = 1;
  249. if (Left->is(TT_Unknown)) {
  250. if (StartsObjCMethodExpr) {
  251. Left->Type = TT_ObjCMethodExpr;
  252. } else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
  253. Contexts.back().ContextKind == tok::l_brace &&
  254. Parent->isOneOf(tok::l_brace, tok::comma)) {
  255. Left->Type = TT_JsComputedPropertyName;
  256. } else if (Parent &&
  257. Parent->isOneOf(tok::at, tok::equal, tok::comma, tok::l_paren,
  258. tok::l_square, tok::question, tok::colon,
  259. tok::kw_return)) {
  260. Left->Type = TT_ArrayInitializerLSquare;
  261. } else {
  262. BindingIncrease = 10;
  263. Left->Type = TT_ArraySubscriptLSquare;
  264. }
  265. }
  266. ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
  267. Contexts.back().IsExpression = true;
  268. Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
  269. while (CurrentToken) {
  270. if (CurrentToken->is(tok::r_square)) {
  271. if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
  272. Left->is(TT_ObjCMethodExpr)) {
  273. // An ObjC method call is rarely followed by an open parenthesis.
  274. // FIXME: Do we incorrectly label ":" with this?
  275. StartsObjCMethodExpr = false;
  276. Left->Type = TT_Unknown;
  277. }
  278. if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
  279. CurrentToken->Type = TT_ObjCMethodExpr;
  280. // determineStarAmpUsage() thinks that '*' '[' is allocating an
  281. // array of pointers, but if '[' starts a selector then '*' is a
  282. // binary operator.
  283. if (Parent && Parent->is(TT_PointerOrReference))
  284. Parent->Type = TT_BinaryOperator;
  285. }
  286. Left->MatchingParen = CurrentToken;
  287. CurrentToken->MatchingParen = Left;
  288. if (Contexts.back().FirstObjCSelectorName) {
  289. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  290. Contexts.back().LongestObjCSelectorName;
  291. if (Left->BlockParameterCount > 1)
  292. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
  293. }
  294. next();
  295. return true;
  296. }
  297. if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
  298. return false;
  299. if (CurrentToken->is(tok::colon)) {
  300. if (Left->is(TT_ArraySubscriptLSquare)) {
  301. Left->Type = TT_ObjCMethodExpr;
  302. StartsObjCMethodExpr = true;
  303. Contexts.back().ColonIsObjCMethodExpr = true;
  304. if (Parent && Parent->is(tok::r_paren))
  305. Parent->Type = TT_CastRParen;
  306. }
  307. ColonFound = true;
  308. }
  309. if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
  310. !ColonFound)
  311. Left->Type = TT_ArrayInitializerLSquare;
  312. FormatToken *Tok = CurrentToken;
  313. if (!consumeToken())
  314. return false;
  315. updateParameterCount(Left, Tok);
  316. }
  317. return false;
  318. }
  319. bool parseBrace() {
  320. if (CurrentToken) {
  321. FormatToken *Left = CurrentToken->Previous;
  322. Left->ParentBracket = Contexts.back().ContextKind;
  323. if (Contexts.back().CaretFound)
  324. Left->Type = TT_ObjCBlockLBrace;
  325. Contexts.back().CaretFound = false;
  326. ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
  327. Contexts.back().ColonIsDictLiteral = true;
  328. if (Left->BlockKind == BK_BracedInit)
  329. Contexts.back().IsExpression = true;
  330. while (CurrentToken) {
  331. if (CurrentToken->is(tok::r_brace)) {
  332. Left->MatchingParen = CurrentToken;
  333. CurrentToken->MatchingParen = Left;
  334. next();
  335. return true;
  336. }
  337. if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
  338. return false;
  339. updateParameterCount(Left, CurrentToken);
  340. if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
  341. FormatToken *Previous = CurrentToken->getPreviousNonComment();
  342. if ((CurrentToken->is(tok::colon) ||
  343. Style.Language == FormatStyle::LK_Proto) &&
  344. Previous->is(tok::identifier))
  345. Previous->Type = TT_SelectorName;
  346. if (CurrentToken->is(tok::colon) ||
  347. Style.Language == FormatStyle::LK_JavaScript)
  348. Left->Type = TT_DictLiteral;
  349. }
  350. if (!consumeToken())
  351. return false;
  352. }
  353. }
  354. return true;
  355. }
  356. void updateParameterCount(FormatToken *Left, FormatToken *Current) {
  357. if (Current->is(TT_LambdaLSquare) ||
  358. (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) ||
  359. (Style.Language == FormatStyle::LK_JavaScript &&
  360. Current->is(Keywords.kw_function))) {
  361. ++Left->BlockParameterCount;
  362. }
  363. if (Current->is(tok::comma)) {
  364. ++Left->ParameterCount;
  365. if (!Left->Role)
  366. Left->Role.reset(new CommaSeparatedList(Style));
  367. Left->Role->CommaFound(Current);
  368. } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
  369. Left->ParameterCount = 1;
  370. }
  371. }
  372. bool parseConditional() {
  373. while (CurrentToken) {
  374. if (CurrentToken->is(tok::colon)) {
  375. CurrentToken->Type = TT_ConditionalExpr;
  376. next();
  377. return true;
  378. }
  379. if (!consumeToken())
  380. return false;
  381. }
  382. return false;
  383. }
  384. bool parseTemplateDeclaration() {
  385. if (CurrentToken && CurrentToken->is(tok::less)) {
  386. CurrentToken->Type = TT_TemplateOpener;
  387. next();
  388. if (!parseAngle())
  389. return false;
  390. if (CurrentToken)
  391. CurrentToken->Previous->ClosesTemplateDeclaration = true;
  392. return true;
  393. }
  394. return false;
  395. }
  396. bool consumeToken() {
  397. FormatToken *Tok = CurrentToken;
  398. next();
  399. switch (Tok->Tok.getKind()) {
  400. case tok::plus:
  401. case tok::minus:
  402. if (!Tok->Previous && Line.MustBeDeclaration)
  403. Tok->Type = TT_ObjCMethodSpecifier;
  404. break;
  405. case tok::colon:
  406. if (!Tok->Previous)
  407. return false;
  408. // Colons from ?: are handled in parseConditional().
  409. if (Style.Language == FormatStyle::LK_JavaScript) {
  410. if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
  411. (Contexts.size() == 1 && // switch/case labels
  412. !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
  413. Contexts.back().ContextKind == tok::l_paren || // function params
  414. Contexts.back().ContextKind == tok::l_square || // array type
  415. (Contexts.size() == 1 &&
  416. Line.MustBeDeclaration)) { // method/property declaration
  417. Tok->Type = TT_JsTypeColon;
  418. break;
  419. }
  420. }
  421. if (Contexts.back().ColonIsDictLiteral) {
  422. Tok->Type = TT_DictLiteral;
  423. } else if (Contexts.back().ColonIsObjCMethodExpr ||
  424. Line.startsWith(TT_ObjCMethodSpecifier)) {
  425. Tok->Type = TT_ObjCMethodExpr;
  426. Tok->Previous->Type = TT_SelectorName;
  427. if (Tok->Previous->ColumnWidth >
  428. Contexts.back().LongestObjCSelectorName) {
  429. Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
  430. }
  431. if (!Contexts.back().FirstObjCSelectorName)
  432. Contexts.back().FirstObjCSelectorName = Tok->Previous;
  433. } else if (Contexts.back().ColonIsForRangeExpr) {
  434. Tok->Type = TT_RangeBasedForLoopColon;
  435. } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
  436. Tok->Type = TT_BitFieldColon;
  437. } else if (Contexts.size() == 1 &&
  438. !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
  439. if (Tok->Previous->is(tok::r_paren))
  440. Tok->Type = TT_CtorInitializerColon;
  441. else
  442. Tok->Type = TT_InheritanceColon;
  443. } else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
  444. Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
  445. // This handles a special macro in ObjC code where selectors including
  446. // the colon are passed as macro arguments.
  447. Tok->Type = TT_ObjCMethodExpr;
  448. } else if (Contexts.back().ContextKind == tok::l_paren) {
  449. Tok->Type = TT_InlineASMColon;
  450. }
  451. break;
  452. case tok::kw_if:
  453. case tok::kw_while:
  454. if (CurrentToken && CurrentToken->is(tok::l_paren)) {
  455. next();
  456. if (!parseParens(/*LookForDecls=*/true))
  457. return false;
  458. }
  459. break;
  460. case tok::kw_for:
  461. Contexts.back().ColonIsForRangeExpr = true;
  462. next();
  463. if (!parseParens())
  464. return false;
  465. break;
  466. case tok::l_paren:
  467. if (!parseParens())
  468. return false;
  469. if (Line.MustBeDeclaration && Contexts.size() == 1 &&
  470. !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
  471. (!Tok->Previous ||
  472. !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
  473. TT_LeadingJavaAnnotation)))
  474. Line.MightBeFunctionDecl = true;
  475. break;
  476. case tok::l_square:
  477. if (!parseSquare())
  478. return false;
  479. break;
  480. case tok::l_brace:
  481. if (!parseBrace())
  482. return false;
  483. break;
  484. case tok::less:
  485. if (!NonTemplateLess.count(Tok) &&
  486. (!Tok->Previous ||
  487. (!Tok->Previous->Tok.isLiteral() &&
  488. !(Tok->Previous->is(tok::r_paren) && Contexts.size() > 1))) &&
  489. parseAngle()) {
  490. Tok->Type = TT_TemplateOpener;
  491. } else {
  492. Tok->Type = TT_BinaryOperator;
  493. NonTemplateLess.insert(Tok);
  494. CurrentToken = Tok;
  495. next();
  496. }
  497. break;
  498. case tok::r_paren:
  499. case tok::r_square:
  500. return false;
  501. case tok::r_brace:
  502. // Lines can start with '}'.
  503. if (Tok->Previous)
  504. return false;
  505. break;
  506. case tok::greater:
  507. Tok->Type = TT_BinaryOperator;
  508. break;
  509. case tok::kw_operator:
  510. while (CurrentToken &&
  511. !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
  512. if (CurrentToken->isOneOf(tok::star, tok::amp))
  513. CurrentToken->Type = TT_PointerOrReference;
  514. consumeToken();
  515. if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
  516. CurrentToken->Previous->Type = TT_OverloadedOperator;
  517. }
  518. if (CurrentToken) {
  519. CurrentToken->Type = TT_OverloadedOperatorLParen;
  520. if (CurrentToken->Previous->is(TT_BinaryOperator))
  521. CurrentToken->Previous->Type = TT_OverloadedOperator;
  522. }
  523. break;
  524. case tok::question:
  525. if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
  526. Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
  527. tok::r_brace)) {
  528. // Question marks before semicolons, colons, etc. indicate optional
  529. // types (fields, parameters), e.g.
  530. // function(x?: string, y?) {...}
  531. // class X { y?; }
  532. Tok->Type = TT_JsTypeOptionalQuestion;
  533. break;
  534. }
  535. // Declarations cannot be conditional expressions, this can only be part
  536. // of a type declaration.
  537. if (Line.MustBeDeclaration &&
  538. Style.Language == FormatStyle::LK_JavaScript)
  539. break;
  540. parseConditional();
  541. break;
  542. case tok::kw_template:
  543. parseTemplateDeclaration();
  544. break;
  545. case tok::comma:
  546. if (Contexts.back().InCtorInitializer)
  547. Tok->Type = TT_CtorInitializerComma;
  548. else if (Contexts.back().FirstStartOfName &&
  549. (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
  550. Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
  551. Line.IsMultiVariableDeclStmt = true;
  552. }
  553. if (Contexts.back().IsForEachMacro)
  554. Contexts.back().IsExpression = true;
  555. break;
  556. default:
  557. break;
  558. }
  559. return true;
  560. }
  561. void parseIncludeDirective() {
  562. if (CurrentToken && CurrentToken->is(tok::less)) {
  563. next();
  564. while (CurrentToken) {
  565. if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
  566. CurrentToken->Type = TT_ImplicitStringLiteral;
  567. next();
  568. }
  569. }
  570. }
  571. void parseWarningOrError() {
  572. next();
  573. // We still want to format the whitespace left of the first token of the
  574. // warning or error.
  575. next();
  576. while (CurrentToken) {
  577. CurrentToken->Type = TT_ImplicitStringLiteral;
  578. next();
  579. }
  580. }
  581. void parsePragma() {
  582. next(); // Consume "pragma".
  583. if (CurrentToken &&
  584. CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
  585. bool IsMark = CurrentToken->is(Keywords.kw_mark);
  586. next(); // Consume "mark".
  587. next(); // Consume first token (so we fix leading whitespace).
  588. while (CurrentToken) {
  589. if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
  590. CurrentToken->Type = TT_ImplicitStringLiteral;
  591. next();
  592. }
  593. }
  594. }
  595. LineType parsePreprocessorDirective() {
  596. LineType Type = LT_PreprocessorDirective;
  597. next();
  598. if (!CurrentToken)
  599. return Type;
  600. if (CurrentToken->Tok.is(tok::numeric_constant)) {
  601. CurrentToken->SpacesRequiredBefore = 1;
  602. return Type;
  603. }
  604. // Hashes in the middle of a line can lead to any strange token
  605. // sequence.
  606. if (!CurrentToken->Tok.getIdentifierInfo())
  607. return Type;
  608. switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
  609. case tok::pp_include:
  610. case tok::pp_include_next:
  611. case tok::pp_import:
  612. next();
  613. parseIncludeDirective();
  614. Type = LT_ImportStatement;
  615. break;
  616. case tok::pp_error:
  617. case tok::pp_warning:
  618. parseWarningOrError();
  619. break;
  620. case tok::pp_pragma:
  621. parsePragma();
  622. break;
  623. case tok::pp_if:
  624. case tok::pp_elif:
  625. Contexts.back().IsExpression = true;
  626. parseLine();
  627. break;
  628. default:
  629. break;
  630. }
  631. while (CurrentToken)
  632. next();
  633. return Type;
  634. }
  635. public:
  636. LineType parseLine() {
  637. NonTemplateLess.clear();
  638. if (CurrentToken->is(tok::hash))
  639. return parsePreprocessorDirective();
  640. // Directly allow to 'import <string-literal>' to support protocol buffer
  641. // definitions (code.google.com/p/protobuf) or missing "#" (either way we
  642. // should not break the line).
  643. IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
  644. if ((Style.Language == FormatStyle::LK_Java &&
  645. CurrentToken->is(Keywords.kw_package)) ||
  646. (Info && Info->getPPKeywordID() == tok::pp_import &&
  647. CurrentToken->Next &&
  648. CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
  649. tok::kw_static))) {
  650. next();
  651. parseIncludeDirective();
  652. return LT_ImportStatement;
  653. }
  654. // If this line starts and ends in '<' and '>', respectively, it is likely
  655. // part of "#define <a/b.h>".
  656. if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
  657. parseIncludeDirective();
  658. return LT_ImportStatement;
  659. }
  660. // In .proto files, top-level options are very similar to import statements
  661. // and should not be line-wrapped.
  662. if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
  663. CurrentToken->is(Keywords.kw_option)) {
  664. next();
  665. if (CurrentToken && CurrentToken->is(tok::identifier))
  666. return LT_ImportStatement;
  667. }
  668. bool KeywordVirtualFound = false;
  669. bool ImportStatement = false;
  670. while (CurrentToken) {
  671. if (CurrentToken->is(tok::kw_virtual))
  672. KeywordVirtualFound = true;
  673. if (IsImportStatement(*CurrentToken))
  674. ImportStatement = true;
  675. if (!consumeToken())
  676. return LT_Invalid;
  677. }
  678. if (KeywordVirtualFound)
  679. return LT_VirtualFunctionDecl;
  680. if (ImportStatement)
  681. return LT_ImportStatement;
  682. if (Line.startsWith(TT_ObjCMethodSpecifier)) {
  683. if (Contexts.back().FirstObjCSelectorName)
  684. Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
  685. Contexts.back().LongestObjCSelectorName;
  686. return LT_ObjCMethodDecl;
  687. }
  688. return LT_Other;
  689. }
  690. private:
  691. bool IsImportStatement(const FormatToken &Tok) {
  692. // FIXME: Closure-library specific stuff should not be hard-coded but be
  693. // configurable.
  694. return Style.Language == FormatStyle::LK_JavaScript &&
  695. Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
  696. Tok.Next->Next && (Tok.Next->Next->TokenText == "module" ||
  697. Tok.Next->Next->TokenText == "require" ||
  698. Tok.Next->Next->TokenText == "provide") &&
  699. Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
  700. }
  701. void resetTokenMetadata(FormatToken *Token) {
  702. if (!Token)
  703. return;
  704. // Reset token type in case we have already looked at it and then
  705. // recovered from an error (e.g. failure to find the matching >).
  706. if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
  707. TT_FunctionLBrace, TT_ImplicitStringLiteral,
  708. TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
  709. TT_RegexLiteral))
  710. CurrentToken->Type = TT_Unknown;
  711. CurrentToken->Role.reset();
  712. CurrentToken->MatchingParen = nullptr;
  713. CurrentToken->FakeLParens.clear();
  714. CurrentToken->FakeRParens = 0;
  715. }
  716. void next() {
  717. if (CurrentToken) {
  718. CurrentToken->NestingLevel = Contexts.size() - 1;
  719. CurrentToken->BindingStrength = Contexts.back().BindingStrength;
  720. modifyContext(*CurrentToken);
  721. determineTokenType(*CurrentToken);
  722. CurrentToken = CurrentToken->Next;
  723. }
  724. resetTokenMetadata(CurrentToken);
  725. }
  726. /// \brief A struct to hold information valid in a specific context, e.g.
  727. /// a pair of parenthesis.
  728. struct Context {
  729. Context(tok::TokenKind ContextKind, unsigned BindingStrength,
  730. bool IsExpression)
  731. : ContextKind(ContextKind), BindingStrength(BindingStrength),
  732. IsExpression(IsExpression) {}
  733. tok::TokenKind ContextKind;
  734. unsigned BindingStrength;
  735. bool IsExpression;
  736. unsigned LongestObjCSelectorName = 0;
  737. bool ColonIsForRangeExpr = false;
  738. bool ColonIsDictLiteral = false;
  739. bool ColonIsObjCMethodExpr = false;
  740. FormatToken *FirstObjCSelectorName = nullptr;
  741. FormatToken *FirstStartOfName = nullptr;
  742. bool CanBeExpression = true;
  743. bool InTemplateArgument = false;
  744. bool InCtorInitializer = false;
  745. bool CaretFound = false;
  746. bool IsForEachMacro = false;
  747. };
  748. /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
  749. /// of each instance.
  750. struct ScopedContextCreator {
  751. AnnotatingParser &P;
  752. ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
  753. unsigned Increase)
  754. : P(P) {
  755. P.Contexts.push_back(Context(ContextKind,
  756. P.Contexts.back().BindingStrength + Increase,
  757. P.Contexts.back().IsExpression));
  758. }
  759. ~ScopedContextCreator() { P.Contexts.pop_back(); }
  760. };
  761. void modifyContext(const FormatToken &Current) {
  762. if (Current.getPrecedence() == prec::Assignment &&
  763. !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
  764. (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
  765. Contexts.back().IsExpression = true;
  766. if (!Line.startsWith(TT_UnaryOperator)) {
  767. for (FormatToken *Previous = Current.Previous;
  768. Previous && !Previous->isOneOf(tok::comma, tok::semi);
  769. Previous = Previous->Previous) {
  770. if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
  771. Previous = Previous->MatchingParen;
  772. if (!Previous)
  773. break;
  774. }
  775. if (Previous->opensScope())
  776. break;
  777. if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
  778. Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
  779. Previous->Previous && Previous->Previous->isNot(tok::equal))
  780. Previous->Type = TT_PointerOrReference;
  781. }
  782. }
  783. } else if (Current.is(tok::lessless) &&
  784. (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
  785. Contexts.back().IsExpression = true;
  786. } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
  787. Contexts.back().IsExpression = true;
  788. } else if (Current.is(TT_TrailingReturnArrow)) {
  789. Contexts.back().IsExpression = false;
  790. } else if (Current.is(TT_LambdaArrow)) {
  791. Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
  792. } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
  793. !Line.InPPDirective &&
  794. (!Current.Previous ||
  795. Current.Previous->isNot(tok::kw_decltype))) {
  796. bool ParametersOfFunctionType =
  797. Current.Previous && Current.Previous->is(tok::r_paren) &&
  798. Current.Previous->MatchingParen &&
  799. Current.Previous->MatchingParen->is(TT_FunctionTypeLParen);
  800. bool IsForOrCatch = Current.Previous &&
  801. Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
  802. Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
  803. } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
  804. for (FormatToken *Previous = Current.Previous;
  805. Previous && Previous->isOneOf(tok::star, tok::amp);
  806. Previous = Previous->Previous)
  807. Previous->Type = TT_PointerOrReference;
  808. if (Line.MustBeDeclaration)
  809. Contexts.back().IsExpression = Contexts.front().InCtorInitializer;
  810. } else if (Current.Previous &&
  811. Current.Previous->is(TT_CtorInitializerColon)) {
  812. Contexts.back().IsExpression = true;
  813. Contexts.back().InCtorInitializer = true;
  814. } else if (Current.is(tok::kw_new)) {
  815. Contexts.back().CanBeExpression = false;
  816. } else if (Current.isOneOf(tok::semi, tok::exclaim)) {
  817. // This should be the condition or increment in a for-loop.
  818. Contexts.back().IsExpression = true;
  819. }
  820. }
  821. void determineTokenType(FormatToken &Current) {
  822. if (!Current.is(TT_Unknown))
  823. // The token type is already known.
  824. return;
  825. // Line.MightBeFunctionDecl can only be true after the parentheses of a
  826. // function declaration have been found. In this case, 'Current' is a
  827. // trailing token of this declaration and thus cannot be a name.
  828. if (Current.is(Keywords.kw_instanceof)) {
  829. Current.Type = TT_BinaryOperator;
  830. } else if (isStartOfName(Current) &&
  831. (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
  832. Contexts.back().FirstStartOfName = &Current;
  833. Current.Type = TT_StartOfName;
  834. } else if (Current.is(tok::kw_auto)) {
  835. AutoFound = true;
  836. } else if (Current.is(tok::arrow) &&
  837. Style.Language == FormatStyle::LK_Java) {
  838. Current.Type = TT_LambdaArrow;
  839. } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
  840. Current.NestingLevel == 0) {
  841. Current.Type = TT_TrailingReturnArrow;
  842. } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
  843. Current.Type =
  844. determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
  845. Contexts.back().IsExpression,
  846. Contexts.back().InTemplateArgument);
  847. } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
  848. Current.Type = determinePlusMinusCaretUsage(Current);
  849. if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
  850. Contexts.back().CaretFound = true;
  851. } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
  852. Current.Type = determineIncrementUsage(Current);
  853. } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
  854. Current.Type = TT_UnaryOperator;
  855. } else if (Current.is(tok::question)) {
  856. if (Style.Language == FormatStyle::LK_JavaScript &&
  857. Line.MustBeDeclaration) {
  858. // In JavaScript, `interface X { foo?(): bar; }` is an optional method
  859. // on the interface, not a ternary expression.
  860. Current.Type = TT_JsTypeOptionalQuestion;
  861. } else {
  862. Current.Type = TT_ConditionalExpr;
  863. }
  864. } else if (Current.isBinaryOperator() &&
  865. (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
  866. Current.Type = TT_BinaryOperator;
  867. } else if (Current.is(tok::comment)) {
  868. if (Current.TokenText.startswith("/*")) {
  869. if (Current.TokenText.endswith("*/"))
  870. Current.Type = TT_BlockComment;
  871. else
  872. // The lexer has for some reason determined a comment here. But we
  873. // cannot really handle it, if it isn't properly terminated.
  874. Current.Tok.setKind(tok::unknown);
  875. } else {
  876. Current.Type = TT_LineComment;
  877. }
  878. } else if (Current.is(tok::r_paren)) {
  879. if (rParenEndsCast(Current))
  880. Current.Type = TT_CastRParen;
  881. if (Current.MatchingParen && Current.Next &&
  882. !Current.Next->isBinaryOperator() &&
  883. !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace))
  884. if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
  885. if (BeforeParen->is(tok::identifier) &&
  886. BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
  887. (!BeforeParen->Previous ||
  888. BeforeParen->Previous->ClosesTemplateDeclaration))
  889. Current.Type = TT_FunctionAnnotationRParen;
  890. } else if (Current.is(tok::at) && Current.Next) {
  891. if (Current.Next->isStringLiteral()) {
  892. Current.Type = TT_ObjCStringLiteral;
  893. } else {
  894. switch (Current.Next->Tok.getObjCKeywordID()) {
  895. case tok::objc_interface:
  896. case tok::objc_implementation:
  897. case tok::objc_protocol:
  898. Current.Type = TT_ObjCDecl;
  899. break;
  900. case tok::objc_property:
  901. Current.Type = TT_ObjCProperty;
  902. break;
  903. default:
  904. break;
  905. }
  906. }
  907. } else if (Current.is(tok::period)) {
  908. FormatToken *PreviousNoComment = Current.getPreviousNonComment();
  909. if (PreviousNoComment &&
  910. PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
  911. Current.Type = TT_DesignatedInitializerPeriod;
  912. else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
  913. Current.Previous->isOneOf(TT_JavaAnnotation,
  914. TT_LeadingJavaAnnotation)) {
  915. Current.Type = Current.Previous->Type;
  916. }
  917. } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
  918. Current.Previous &&
  919. !Current.Previous->isOneOf(tok::equal, tok::at) &&
  920. Line.MightBeFunctionDecl && Contexts.size() == 1) {
  921. // Line.MightBeFunctionDecl can only be true after the parentheses of a
  922. // function declaration have been found.
  923. Current.Type = TT_TrailingAnnotation;
  924. } else if ((Style.Language == FormatStyle::LK_Java ||
  925. Style.Language == FormatStyle::LK_JavaScript) &&
  926. Current.Previous) {
  927. if (Current.Previous->is(tok::at) &&
  928. Current.isNot(Keywords.kw_interface)) {
  929. const FormatToken &AtToken = *Current.Previous;
  930. const FormatToken *Previous = AtToken.getPreviousNonComment();
  931. if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
  932. Current.Type = TT_LeadingJavaAnnotation;
  933. else
  934. Current.Type = TT_JavaAnnotation;
  935. } else if (Current.Previous->is(tok::period) &&
  936. Current.Previous->isOneOf(TT_JavaAnnotation,
  937. TT_LeadingJavaAnnotation)) {
  938. Current.Type = Current.Previous->Type;
  939. }
  940. }
  941. }
  942. /// \brief Take a guess at whether \p Tok starts a name of a function or
  943. /// variable declaration.
  944. ///
  945. /// This is a heuristic based on whether \p Tok is an identifier following
  946. /// something that is likely a type.
  947. bool isStartOfName(const FormatToken &Tok) {
  948. if (Tok.isNot(tok::identifier) || !Tok.Previous)
  949. return false;
  950. if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof))
  951. return false;
  952. // Skip "const" as it does not have an influence on whether this is a name.
  953. FormatToken *PreviousNotConst = Tok.Previous;
  954. while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
  955. PreviousNotConst = PreviousNotConst->Previous;
  956. if (!PreviousNotConst)
  957. return false;
  958. bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
  959. PreviousNotConst->Previous &&
  960. PreviousNotConst->Previous->is(tok::hash);
  961. if (PreviousNotConst->is(TT_TemplateCloser))
  962. return PreviousNotConst && PreviousNotConst->MatchingParen &&
  963. PreviousNotConst->MatchingParen->Previous &&
  964. PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
  965. PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
  966. if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
  967. PreviousNotConst->MatchingParen->Previous &&
  968. PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
  969. return true;
  970. return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
  971. PreviousNotConst->is(TT_PointerOrReference) ||
  972. PreviousNotConst->isSimpleTypeSpecifier();
  973. }
  974. /// \brief Determine whether ')' is ending a cast.
  975. bool rParenEndsCast(const FormatToken &Tok) {
  976. FormatToken *LeftOfParens = nullptr;
  977. if (Tok.MatchingParen)
  978. LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
  979. if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
  980. LeftOfParens->MatchingParen)
  981. LeftOfParens = LeftOfParens->MatchingParen->Previous;
  982. if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
  983. LeftOfParens->MatchingParen &&
  984. LeftOfParens->MatchingParen->is(TT_LambdaLSquare))
  985. return false;
  986. if (Tok.Next) {
  987. if (Tok.Next->is(tok::question))
  988. return false;
  989. if (Style.Language == FormatStyle::LK_JavaScript &&
  990. Tok.Next->is(Keywords.kw_in))
  991. return false;
  992. if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
  993. return true;
  994. }
  995. bool IsCast = false;
  996. bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
  997. bool ParensAreType =
  998. !Tok.Previous ||
  999. Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
  1000. Tok.Previous->isSimpleTypeSpecifier();
  1001. bool ParensCouldEndDecl =
  1002. Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
  1003. bool IsSizeOfOrAlignOf =
  1004. LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
  1005. if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
  1006. (Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression))
  1007. IsCast = true;
  1008. else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
  1009. (Tok.Next->Tok.isLiteral() ||
  1010. Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
  1011. IsCast = true;
  1012. // If there is an identifier after the (), it is likely a cast, unless
  1013. // there is also an identifier before the ().
  1014. else if (LeftOfParens && Tok.Next &&
  1015. (LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
  1016. LeftOfParens->isOneOf(tok::kw_return, tok::kw_case)) &&
  1017. !LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at,
  1018. TT_TemplateCloser)) {
  1019. if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
  1020. IsCast = true;
  1021. } else {
  1022. // Use heuristics to recognize c style casting.
  1023. FormatToken *Prev = Tok.Previous;
  1024. if (Prev && Prev->isOneOf(tok::amp, tok::star))
  1025. Prev = Prev->Previous;
  1026. if (Prev && Tok.Next && Tok.Next->Next) {
  1027. bool NextIsUnary = Tok.Next->isUnaryOperator() ||
  1028. Tok.Next->isOneOf(tok::amp, tok::star);
  1029. IsCast =
  1030. NextIsUnary && !Tok.Next->is(tok::plus) &&
  1031. Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
  1032. }
  1033. for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
  1034. if (!Prev ||
  1035. !Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) {
  1036. IsCast = false;
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. return IsCast && !ParensAreEmpty;
  1043. }
  1044. /// \brief Return the type of the given token assuming it is * or &.
  1045. TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
  1046. bool InTemplateArgument) {
  1047. if (Style.Language == FormatStyle::LK_JavaScript)
  1048. return TT_BinaryOperator;
  1049. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1050. if (!PrevToken)
  1051. return TT_UnaryOperator;
  1052. const FormatToken *NextToken = Tok.getNextNonComment();
  1053. if (!NextToken || NextToken->is(tok::arrow) ||
  1054. (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
  1055. return TT_Unknown;
  1056. if (PrevToken->is(tok::coloncolon))
  1057. return TT_PointerOrReference;
  1058. if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
  1059. tok::comma, tok::semi, tok::kw_return, tok::colon,
  1060. tok::equal, tok::kw_delete, tok::kw_sizeof) ||
  1061. PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
  1062. TT_UnaryOperator, TT_CastRParen))
  1063. return TT_UnaryOperator;
  1064. if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
  1065. return TT_PointerOrReference;
  1066. if (NextToken->isOneOf(tok::kw_operator, tok::comma, tok::semi))
  1067. return TT_PointerOrReference;
  1068. if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
  1069. PrevToken->MatchingParen->Previous &&
  1070. PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
  1071. tok::kw_decltype))
  1072. return TT_PointerOrReference;
  1073. if (PrevToken->Tok.isLiteral() ||
  1074. PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
  1075. tok::kw_false, tok::r_brace) ||
  1076. NextToken->Tok.isLiteral() ||
  1077. NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
  1078. NextToken->isUnaryOperator() ||
  1079. // If we know we're in a template argument, there are no named
  1080. // declarations. Thus, having an identifier on the right-hand side
  1081. // indicates a binary operator.
  1082. (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
  1083. return TT_BinaryOperator;
  1084. // "&&(" is quite unlikely to be two successive unary "&".
  1085. if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
  1086. return TT_BinaryOperator;
  1087. // This catches some cases where evaluation order is used as control flow:
  1088. // aaa && aaa->f();
  1089. const FormatToken *NextNextToken = NextToken->getNextNonComment();
  1090. if (NextNextToken && NextNextToken->is(tok::arrow))
  1091. return TT_BinaryOperator;
  1092. // It is very unlikely that we are going to find a pointer or reference type
  1093. // definition on the RHS of an assignment.
  1094. if (IsExpression && !Contexts.back().CaretFound)
  1095. return TT_BinaryOperator;
  1096. return TT_PointerOrReference;
  1097. }
  1098. TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
  1099. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1100. if (!PrevToken || PrevToken->is(TT_CastRParen))
  1101. return TT_UnaryOperator;
  1102. // Use heuristics to recognize unary operators.
  1103. if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
  1104. tok::question, tok::colon, tok::kw_return,
  1105. tok::kw_case, tok::at, tok::l_brace))
  1106. return TT_UnaryOperator;
  1107. // There can't be two consecutive binary operators.
  1108. if (PrevToken->is(TT_BinaryOperator))
  1109. return TT_UnaryOperator;
  1110. // Fall back to marking the token as binary operator.
  1111. return TT_BinaryOperator;
  1112. }
  1113. /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
  1114. TokenType determineIncrementUsage(const FormatToken &Tok) {
  1115. const FormatToken *PrevToken = Tok.getPreviousNonComment();
  1116. if (!PrevToken || PrevToken->is(TT_CastRParen))
  1117. return TT_UnaryOperator;
  1118. if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
  1119. return TT_TrailingUnaryOperator;
  1120. return TT_UnaryOperator;
  1121. }
  1122. SmallVector<Context, 8> Contexts;
  1123. const FormatStyle &Style;
  1124. AnnotatedLine &Line;
  1125. FormatToken *CurrentToken;
  1126. bool AutoFound;
  1127. const AdditionalKeywords &Keywords;
  1128. // Set of "<" tokens that do not open a template parameter list. If parseAngle
  1129. // determines that a specific token can't be a template opener, it will make
  1130. // same decision irrespective of the decisions for tokens leading up to it.
  1131. // Store this information to prevent this from causing exponential runtime.
  1132. llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
  1133. };
  1134. static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
  1135. static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
  1136. /// \brief Parses binary expressions by inserting fake parenthesis based on
  1137. /// operator precedence.
  1138. class ExpressionParser {
  1139. public:
  1140. ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
  1141. AnnotatedLine &Line)
  1142. : Style(Style), Keywords(Keywords), Current(Line.First) {}
  1143. /// \brief Parse expressions with the given operatore precedence.
  1144. void parse(int Precedence = 0) {
  1145. // Skip 'return' and ObjC selector colons as they are not part of a binary
  1146. // expression.
  1147. while (Current && (Current->is(tok::kw_return) ||
  1148. (Current->is(tok::colon) &&
  1149. Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
  1150. next();
  1151. if (!Current || Precedence > PrecedenceArrowAndPeriod)
  1152. return;
  1153. // Conditional expressions need to be parsed separately for proper nesting.
  1154. if (Precedence == prec::Conditional) {
  1155. parseConditionalExpr();
  1156. return;
  1157. }
  1158. // Parse unary operators, which all have a higher precedence than binary
  1159. // operators.
  1160. if (Precedence == PrecedenceUnaryOperator) {
  1161. parseUnaryOperator();
  1162. return;
  1163. }
  1164. FormatToken *Start = Current;
  1165. FormatToken *LatestOperator = nullptr;
  1166. unsigned OperatorIndex = 0;
  1167. while (Current) {
  1168. // Consume operators with higher precedence.
  1169. parse(Precedence + 1);
  1170. int CurrentPrecedence = getCurrentPrecedence();
  1171. if (Current && Current->is(TT_SelectorName) &&
  1172. Precedence == CurrentPrecedence) {
  1173. if (LatestOperator)
  1174. addFakeParenthesis(Start, prec::Level(Precedence));
  1175. Start = Current;
  1176. }
  1177. // At the end of the line or when an operator with higher precedence is
  1178. // found, insert fake parenthesis and return.
  1179. if (!Current || (Current->closesScope() && Current->MatchingParen) ||
  1180. (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
  1181. (CurrentPrecedence == prec::Conditional &&
  1182. Precedence == prec::Assignment && Current->is(tok::colon))) {
  1183. break;
  1184. }
  1185. // Consume scopes: (), [], <> and {}
  1186. if (Current->opensScope()) {
  1187. while (Current && !Current->closesScope()) {
  1188. next();
  1189. parse();
  1190. }
  1191. next();
  1192. } else {
  1193. // Operator found.
  1194. if (CurrentPrecedence == Precedence) {
  1195. LatestOperator = Current;
  1196. Current->OperatorIndex = OperatorIndex;
  1197. ++OperatorIndex;
  1198. }
  1199. next(/*SkipPastLeadingComments=*/Precedence > 0);
  1200. }
  1201. }
  1202. if (LatestOperator && (Current || Precedence > 0)) {
  1203. LatestOperator->LastOperator = true;
  1204. if (Precedence == PrecedenceArrowAndPeriod) {
  1205. // Call expressions don't have a binary operator precedence.
  1206. addFakeParenthesis(Start, prec::Unknown);
  1207. } else {
  1208. addFakeParenthesis(Start, prec::Level(Precedence));
  1209. }
  1210. }
  1211. }
  1212. private:
  1213. /// \brief Gets the precedence (+1) of the given token for binary operators
  1214. /// and other tokens that we treat like binary operators.
  1215. int getCurrentPrecedence() {
  1216. if (Current) {
  1217. const FormatToken *NextNonComment = Current->getNextNonComment();
  1218. if (Current->is(TT_ConditionalExpr))
  1219. return prec::Conditional;
  1220. if (NextNonComment && NextNonComment->is(tok::colon) &&
  1221. NextNonComment->is(TT_DictLiteral))
  1222. return prec::Comma;
  1223. if (Current->is(TT_LambdaArrow))
  1224. return prec::Comma;
  1225. if (Current->is(TT_JsFatArrow))
  1226. return prec::Assignment;
  1227. if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName,
  1228. TT_JsComputedPropertyName) ||
  1229. (Current->is(tok::comment) && NextNonComment &&
  1230. NextNonComment->is(TT_SelectorName)))
  1231. return 0;
  1232. if (Current->is(TT_RangeBasedForLoopColon))
  1233. return prec::Comma;
  1234. if ((Style.Language == FormatStyle::LK_Java ||
  1235. Style.Language == FormatStyle::LK_JavaScript) &&
  1236. Current->is(Keywords.kw_instanceof))
  1237. return prec::Relational;
  1238. if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
  1239. return Current->getPrecedence();
  1240. if (Current->isOneOf(tok::period, tok::arrow))
  1241. return PrecedenceArrowAndPeriod;
  1242. if (Style.Language == FormatStyle::LK_Java &&
  1243. Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
  1244. Keywords.kw_throws))
  1245. return 0;
  1246. }
  1247. return -1;
  1248. }
  1249. void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
  1250. Start->FakeLParens.push_back(Precedence);
  1251. if (Precedence > prec::Unknown)
  1252. Start->StartsBinaryExpression = true;
  1253. if (Current) {
  1254. FormatToken *Previous = Current->Previous;
  1255. while (Previous->is(tok::comment) && Previous->Previous)
  1256. Previous = Previous->Previous;
  1257. ++Previous->FakeRParens;
  1258. if (Precedence > prec::Unknown)
  1259. Previous->EndsBinaryExpression = true;
  1260. }
  1261. }
  1262. /// \brief Parse unary operator expressions and surround them with fake
  1263. /// parentheses if appropriate.
  1264. void parseUnaryOperator() {
  1265. if (!Current || Current->isNot(TT_UnaryOperator)) {
  1266. parse(PrecedenceArrowAndPeriod);
  1267. return;
  1268. }
  1269. FormatToken *Start = Current;
  1270. next();
  1271. parseUnaryOperator();
  1272. // The actual precedence doesn't matter.
  1273. addFakeParenthesis(Start, prec::Unknown);
  1274. }
  1275. void parseConditionalExpr() {
  1276. while (Current && Current->isTrailingComment()) {
  1277. next();
  1278. }
  1279. FormatToken *Start = Current;
  1280. parse(prec::LogicalOr);
  1281. if (!Current || !Current->is(tok::question))
  1282. return;
  1283. next();
  1284. parse(prec::Assignment);
  1285. if (!Current || Current->isNot(TT_ConditionalExpr))
  1286. return;
  1287. next();
  1288. parse(prec::Assignment);
  1289. addFakeParenthesis(Start, prec::Conditional);
  1290. }
  1291. void next(bool SkipPastLeadingComments = true) {
  1292. if (Current)
  1293. Current = Current->Next;
  1294. while (Current &&
  1295. (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
  1296. Current->isTrailingComment())
  1297. Current = Current->Next;
  1298. }
  1299. const FormatStyle &Style;
  1300. const AdditionalKeywords &Keywords;
  1301. FormatToken *Current;
  1302. };
  1303. } // end anonymous namespace
  1304. void TokenAnnotator::setCommentLineLevels(
  1305. SmallVectorImpl<AnnotatedLine *> &Lines) {
  1306. const AnnotatedLine *NextNonCommentLine = nullptr;
  1307. for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
  1308. E = Lines.rend();
  1309. I != E; ++I) {
  1310. if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
  1311. (*I)->First->Next == nullptr)
  1312. (*I)->Level = NextNonCommentLine->Level;
  1313. else
  1314. NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
  1315. setCommentLineLevels((*I)->Children);
  1316. }
  1317. }
  1318. void TokenAnnotator::annotate(AnnotatedLine &Line) {
  1319. for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
  1320. E = Line.Children.end();
  1321. I != E; ++I) {
  1322. annotate(**I);
  1323. }
  1324. AnnotatingParser Parser(Style, Line, Keywords);
  1325. Line.Type = Parser.parseLine();
  1326. if (Line.Type == LT_Invalid)
  1327. return;
  1328. ExpressionParser ExprParser(Style, Keywords, Line);
  1329. ExprParser.parse();
  1330. if (Line.startsWith(TT_ObjCMethodSpecifier))
  1331. Line.Type = LT_ObjCMethodDecl;
  1332. else if (Line.startsWith(TT_ObjCDecl))
  1333. Line.Type = LT_ObjCDecl;
  1334. else if (Line.startsWith(TT_ObjCProperty))
  1335. Line.Type = LT_ObjCProperty;
  1336. Line.First->SpacesRequiredBefore = 1;
  1337. Line.First->CanBreakBefore = Line.First->MustBreakBefore;
  1338. }
  1339. // This function heuristically determines whether 'Current' starts the name of a
  1340. // function declaration.
  1341. static bool isFunctionDeclarationName(const FormatToken &Current) {
  1342. if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
  1343. return false;
  1344. const FormatToken *Next = Current.Next;
  1345. for (; Next; Next = Next->Next) {
  1346. if (Next->is(TT_TemplateOpener)) {
  1347. Next = Next->MatchingParen;
  1348. } else if (Next->is(tok::coloncolon)) {
  1349. Next = Next->Next;
  1350. if (!Next || !Next->is(tok::identifier))
  1351. return false;
  1352. } else if (Next->is(tok::l_paren)) {
  1353. break;
  1354. } else {
  1355. return false;
  1356. }
  1357. }
  1358. if (!Next)
  1359. return false;
  1360. assert(Next->is(tok::l_paren));
  1361. if (Next->Next == Next->MatchingParen)
  1362. return true;
  1363. for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
  1364. Tok = Tok->Next) {
  1365. if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
  1366. Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
  1367. return true;
  1368. if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
  1369. Tok->Tok.isLiteral())
  1370. return false;
  1371. }
  1372. return false;
  1373. }
  1374. void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
  1375. for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
  1376. E = Line.Children.end();
  1377. I != E; ++I) {
  1378. calculateFormattingInformation(**I);
  1379. }
  1380. Line.First->TotalLength =
  1381. Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
  1382. if (!Line.First->Next)
  1383. return;
  1384. FormatToken *Current = Line.First->Next;
  1385. bool InFunctionDecl = Line.MightBeFunctionDecl;
  1386. while (Current) {
  1387. if (isFunctionDeclarationName(*Current))
  1388. Current->Type = TT_FunctionDeclarationName;
  1389. if (Current->is(TT_LineComment)) {
  1390. if (Current->Previous->BlockKind == BK_BracedInit &&
  1391. Current->Previous->opensScope())
  1392. Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
  1393. else
  1394. Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
  1395. // If we find a trailing comment, iterate backwards to determine whether
  1396. // it seems to relate to a specific parameter. If so, break before that
  1397. // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
  1398. // to the previous line in:
  1399. // SomeFunction(a,
  1400. // b, // comment
  1401. // c);
  1402. if (!Current->HasUnescapedNewline) {
  1403. for (FormatToken *Parameter = Current->Previous; Parameter;
  1404. Parameter = Parameter->Previous) {
  1405. if (Parameter->isOneOf(tok::comment, tok::r_brace))
  1406. break;
  1407. if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
  1408. if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
  1409. Parameter->HasUnescapedNewline)
  1410. Parameter->MustBreakBefore = true;
  1411. break;
  1412. }
  1413. }
  1414. }
  1415. } else if (Current->SpacesRequiredBefore == 0 &&
  1416. spaceRequiredBefore(Line, *Current)) {
  1417. Current->SpacesRequiredBefore = 1;
  1418. }
  1419. Current->MustBreakBefore =
  1420. Current->MustBreakBefore || mustBreakBefore(Line, *Current);
  1421. if ((Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All ||
  1422. (Style.AlwaysBreakAfterDefinitionReturnType ==
  1423. FormatStyle::DRTBS_TopLevel &&
  1424. Line.Level == 0)) &&
  1425. InFunctionDecl && Current->is(TT_FunctionDeclarationName) &&
  1426. !Line.Last->isOneOf(tok::semi, tok::comment)) // Only for definitions.
  1427. // FIXME: Line.Last points to other characters than tok::semi
  1428. // and tok::lbrace.
  1429. Current->MustBreakBefore = true;
  1430. Current->CanBreakBefore =
  1431. Current->MustBreakBefore || canBreakBefore(Line, *Current);
  1432. unsigned ChildSize = 0;
  1433. if (Current->Previous->Children.size() == 1) {
  1434. FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
  1435. ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
  1436. : LastOfChild.TotalLength + 1;
  1437. }
  1438. const FormatToken *Prev = Current->Previous;
  1439. if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
  1440. (Prev->Children.size() == 1 &&
  1441. Prev->Children[0]->First->MustBreakBefore) ||
  1442. Current->IsMultiline)
  1443. Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
  1444. else
  1445. Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
  1446. ChildSize + Current->SpacesRequiredBefore;
  1447. if (Current->is(TT_CtorInitializerColon))
  1448. InFunctionDecl = false;
  1449. // FIXME: Only calculate this if CanBreakBefore is true once static
  1450. // initializers etc. are sorted out.
  1451. // FIXME: Move magic numbers to a better place.
  1452. Current->SplitPenalty = 20 * Current->BindingStrength +
  1453. splitPenalty(Line, *Current, InFunctionDecl);
  1454. Current = Current->Next;
  1455. }
  1456. calculateUnbreakableTailLengths(Line);
  1457. for (Current = Line.First; Current != nullptr; Current = Current->Next) {
  1458. if (Current->Role)
  1459. Current->Role->precomputeFormattingInfos(Current);
  1460. }
  1461. DEBUG({ printDebugInfo(Line); });
  1462. }
  1463. void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
  1464. unsigned UnbreakableTailLength = 0;
  1465. FormatToken *Current = Line.Last;
  1466. while (Current) {
  1467. Current->UnbreakableTailLength = UnbreakableTailLength;
  1468. if (Current->CanBreakBefore ||
  1469. Current->isOneOf(tok::comment, tok::string_literal)) {
  1470. UnbreakableTailLength = 0;
  1471. } else {
  1472. UnbreakableTailLength +=
  1473. Current->ColumnWidth + Current->SpacesRequiredBefore;
  1474. }
  1475. Current = Current->Previous;
  1476. }
  1477. }
  1478. unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
  1479. const FormatToken &Tok,
  1480. bool InFunctionDecl) {
  1481. const FormatToken &Left = *Tok.Previous;
  1482. const FormatToken &Right = Tok;
  1483. if (Left.is(tok::semi))
  1484. return 0;
  1485. if (Style.Language == FormatStyle::LK_Java) {
  1486. if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
  1487. return 1;
  1488. if (Right.is(Keywords.kw_implements))
  1489. return 2;
  1490. if (Left.is(tok::comma) && Left.NestingLevel == 0)
  1491. return 3;
  1492. } else if (Style.Language == FormatStyle::LK_JavaScript) {
  1493. if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
  1494. return 100;
  1495. if (Left.is(TT_JsTypeColon))
  1496. return 100;
  1497. }
  1498. if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
  1499. Right.Next->is(TT_DictLiteral)))
  1500. return 1;
  1501. if (Right.is(tok::l_square)) {
  1502. if (Style.Language == FormatStyle::LK_Proto)
  1503. return 1;
  1504. // Slightly prefer formatting local lambda definitions like functions.
  1505. if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
  1506. return 50;
  1507. if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
  1508. TT_ArrayInitializerLSquare))
  1509. return 500;
  1510. }
  1511. if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
  1512. Right.is(tok::kw_operator)) {
  1513. if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
  1514. return 3;
  1515. if (Left.is(TT_StartOfName))
  1516. return 110;
  1517. if (InFunctionDecl && Right.NestingLevel == 0)
  1518. return Style.PenaltyReturnTypeOnItsOwnLine;
  1519. return 200;
  1520. }
  1521. if (Right.is(TT_PointerOrReference))
  1522. return 190;
  1523. if (Right.is(TT_LambdaArrow))
  1524. return 110;
  1525. if (Left.is(tok::equal) && Right.is(tok::l_brace))
  1526. return 150;
  1527. if (Left.is(TT_CastRParen))
  1528. return 100;
  1529. if (Left.is(tok::coloncolon) ||
  1530. (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
  1531. return 500;
  1532. if (Left.isOneOf(tok::kw_class, tok::kw_struct))
  1533. return 5000;
  1534. if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
  1535. return 2;
  1536. if (Right.isMemberAccess()) {
  1537. if (Left.is(tok::r_paren) && Left.MatchingParen &&
  1538. Left.MatchingParen->ParameterCount > 0)
  1539. return 20; // Should be smaller than breaking at a nested comma.
  1540. return 150;
  1541. }
  1542. if (Right.is(TT_TrailingAnnotation) &&
  1543. (!Right.Next || Right.Next->isNot(tok::l_paren))) {
  1544. // Moving trailing annotations to the next line is fine for ObjC method
  1545. // declarations.
  1546. if (Line.startsWith(TT_ObjCMethodSpecifier))
  1547. return 10;
  1548. // Generally, breaking before a trailing annotation is bad unless it is
  1549. // function-like. It seems to be especially preferable to keep standard
  1550. // annotations (i.e. "const", "final" and "override") on the same line.
  1551. // Use a slightly higher penalty after ")" so that annotations like
  1552. // "const override" are kept together.
  1553. bool is_short_annotation = Right.TokenText.size() < 10;
  1554. return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
  1555. }
  1556. // In for-loops, prefer breaking at ',' and ';'.
  1557. if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
  1558. return 4;
  1559. // In Objective-C method expressions, prefer breaking before "param:" over
  1560. // breaking after it.
  1561. if (Right.is(TT_SelectorName))
  1562. return 0;
  1563. if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
  1564. return Line.MightBeFunctionDecl ? 50 : 500;
  1565. if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
  1566. return 100;
  1567. if (Left.is(tok::l_paren) && Left.Previous &&
  1568. Left.Previous->isOneOf(tok::kw_if, tok::kw_for))
  1569. return 1000;
  1570. if (Left.is(tok::equal) && InFunctionDecl)
  1571. return 110;
  1572. if (Right.is(tok::r_brace))
  1573. return 1;
  1574. if (Left.is(TT_TemplateOpener))
  1575. return 100;
  1576. if (Left.opensScope()) {
  1577. if (!Style.AlignAfterOpenBracket)
  1578. return 0;
  1579. return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
  1580. : 19;
  1581. }
  1582. if (Left.is(TT_JavaAnnotation))
  1583. return 50;
  1584. if (Right.is(tok::lessless)) {
  1585. if (Left.is(tok::string_literal) &&
  1586. (!Right.LastOperator || Right.OperatorIndex != 1)) {
  1587. StringRef Content = Left.TokenText;
  1588. if (Content.startswith("\""))
  1589. Content = Content.drop_front(1);
  1590. if (Content.endswith("\""))
  1591. Content = Content.drop_back(1);
  1592. Content = Content.trim();
  1593. if (Content.size() > 1 &&
  1594. (Content.back() == ':' || Content.back() == '='))
  1595. return 25;
  1596. }
  1597. return 1; // Breaking at a << is really cheap.
  1598. }
  1599. if (Left.is(TT_ConditionalExpr))
  1600. return prec::Conditional;
  1601. prec::Level Level = Left.getPrecedence();
  1602. if (Level != prec::Unknown)
  1603. return Level;
  1604. Level = Right.getPrecedence();
  1605. if (Level != prec::Unknown)
  1606. return Level;
  1607. return 3;
  1608. }
  1609. bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
  1610. const FormatToken &Left,
  1611. const FormatToken &Right) {
  1612. if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
  1613. return true;
  1614. if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
  1615. Left.Tok.getObjCKeywordID() == tok::objc_property)
  1616. return true;
  1617. if (Right.is(tok::hashhash))
  1618. return Left.is(tok::hash);
  1619. if (Left.isOneOf(tok::hashhash, tok::hash))
  1620. return Right.is(tok::hash);
  1621. if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
  1622. return Style.SpaceInEmptyParentheses;
  1623. if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
  1624. return (Right.is(TT_CastRParen) ||
  1625. (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
  1626. ? Style.SpacesInCStyleCastParentheses
  1627. : Style.SpacesInParentheses;
  1628. if (Right.isOneOf(tok::semi, tok::comma))
  1629. return false;
  1630. if (Right.is(tok::less) &&
  1631. (Left.is(tok::kw_template) ||
  1632. (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
  1633. return true;
  1634. if (Left.isOneOf(tok::exclaim, tok::tilde))
  1635. return false;
  1636. if (Left.is(tok::at) &&
  1637. Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
  1638. tok::numeric_constant, tok::l_paren, tok::l_brace,
  1639. tok::kw_true, tok::kw_false))
  1640. return false;
  1641. if (Left.is(tok::coloncolon))
  1642. return false;
  1643. if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
  1644. return false;
  1645. if (Right.is(tok::ellipsis))
  1646. return Left.Tok.isLiteral();
  1647. if (Left.is(tok::l_square) && Right.is(tok::amp))
  1648. return false;
  1649. if (Right.is(TT_PointerOrReference))
  1650. return !(Left.is(tok::r_paren) && Left.MatchingParen &&
  1651. (Left.MatchingParen->is(TT_OverloadedOperatorLParen) ||
  1652. (Left.MatchingParen->Previous &&
  1653. Left.MatchingParen->Previous->is(
  1654. TT_FunctionDeclarationName)))) &&
  1655. (Left.Tok.isLiteral() ||
  1656. (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
  1657. (Style.PointerAlignment != FormatStyle::PAS_Left ||
  1658. Line.IsMultiVariableDeclStmt)));
  1659. if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
  1660. (!Left.is(TT_PointerOrReference) ||
  1661. (Style.PointerAlignment != FormatStyle::PAS_Right &&
  1662. !Line.IsMultiVariableDeclStmt)))
  1663. return true;
  1664. if (Left.is(TT_PointerOrReference))
  1665. return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
  1666. (Right.is(tok::l_brace) && Right.BlockKind == BK_Block) ||
  1667. (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
  1668. tok::l_paren) &&
  1669. (Style.PointerAlignment != FormatStyle::PAS_Right &&
  1670. !Line.IsMultiVariableDeclStmt) &&
  1671. Left.Previous &&
  1672. !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
  1673. if (Right.is(tok::star) && Left.is(tok::l_paren))
  1674. return false;
  1675. if (Left.is(tok::l_square))
  1676. return (Left.is(TT_ArrayInitializerLSquare) &&
  1677. Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
  1678. (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
  1679. Right.isNot(tok::r_square));
  1680. if (Right.is(tok::r_square))
  1681. return Right.MatchingParen &&
  1682. ((Style.SpacesInContainerLiterals &&
  1683. Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
  1684. (Style.SpacesInSquareBrackets &&
  1685. Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
  1686. if (Right.is(tok::l_square) &&
  1687. !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
  1688. !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
  1689. return false;
  1690. if (Left.is(tok::colon))
  1691. return !Left.is(TT_ObjCMethodExpr);
  1692. if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
  1693. return !Left.Children.empty(); // No spaces in "{}".
  1694. if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
  1695. (Right.is(tok::r_brace) && Right.MatchingParen &&
  1696. Right.MatchingParen->BlockKind != BK_Block))
  1697. return !Style.Cpp11BracedListStyle;
  1698. if (Left.is(TT_BlockComment))
  1699. return !Left.TokenText.endswith("=*/");
  1700. if (Right.is(tok::l_paren)) {
  1701. if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
  1702. return true;
  1703. return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
  1704. (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
  1705. (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
  1706. tok::kw_switch, tok::kw_case, TT_ForEachMacro) ||
  1707. (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
  1708. tok::kw_new, tok::kw_delete) &&
  1709. (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
  1710. (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
  1711. (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
  1712. Left.is(tok::r_paren)) &&
  1713. Line.Type != LT_PreprocessorDirective);
  1714. }
  1715. if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
  1716. return false;
  1717. if (Right.is(TT_UnaryOperator))
  1718. return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
  1719. (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
  1720. if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
  1721. tok::r_paren) ||
  1722. Left.isSimpleTypeSpecifier()) &&
  1723. Right.is(tok::l_brace) && Right.getNextNonComment() &&
  1724. Right.BlockKind != BK_Block)
  1725. return false;
  1726. if (Left.is(tok::period) || Right.is(tok::period))
  1727. return false;
  1728. if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
  1729. return false;
  1730. if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
  1731. Left.MatchingParen->Previous &&
  1732. Left.MatchingParen->Previous->is(tok::period))
  1733. // A.<B>DoSomething();
  1734. return false;
  1735. if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
  1736. return false;
  1737. return true;
  1738. }
  1739. bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
  1740. const FormatToken &Right) {
  1741. const FormatToken &Left = *Right.Previous;
  1742. if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
  1743. return true; // Never ever merge two identifiers.
  1744. if (Style.Language == FormatStyle::LK_Cpp) {
  1745. if (Left.is(tok::kw_operator))
  1746. return Right.is(tok::coloncolon);
  1747. } else if (Style.Language == FormatStyle::LK_Proto) {
  1748. if (Right.is(tok::period) &&
  1749. Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
  1750. Keywords.kw_repeated))
  1751. return true;
  1752. if (Right.is(tok::l_paren) &&
  1753. Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
  1754. return true;
  1755. } else if (Style.Language == FormatStyle::LK_JavaScript) {
  1756. if (Left.isOneOf(Keywords.kw_var, TT_JsFatArrow))
  1757. return true;
  1758. if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
  1759. return false;
  1760. if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
  1761. Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
  1762. return false;
  1763. if (Left.is(tok::ellipsis))
  1764. return false;
  1765. if (Left.is(TT_TemplateCloser) &&
  1766. !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
  1767. Keywords.kw_implements, Keywords.kw_extends))
  1768. // Type assertions ('<type>expr') are not followed by whitespace. Other
  1769. // locations that should have whitespace following are identified by the
  1770. // above set of follower tokens.
  1771. return false;
  1772. } else if (Style.Language == FormatStyle::LK_Java) {
  1773. if (Left.is(tok::r_square) && Right.is(tok::l_brace))
  1774. return true;
  1775. if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
  1776. return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
  1777. if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
  1778. tok::kw_protected) ||
  1779. Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
  1780. Keywords.kw_native)) &&
  1781. Right.is(TT_TemplateOpener))
  1782. return true;
  1783. }
  1784. if (Left.is(TT_ImplicitStringLiteral))
  1785. return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
  1786. if (Line.Type == LT_ObjCMethodDecl) {
  1787. if (Left.is(TT_ObjCMethodSpecifier))
  1788. return true;
  1789. if (Left.is(tok::r_paren) && Right.is(tok::identifier))
  1790. // Don't space between ')' and <id>
  1791. return false;
  1792. }
  1793. if (Line.Type == LT_ObjCProperty &&
  1794. (Right.is(tok::equal) || Left.is(tok::equal)))
  1795. return false;
  1796. if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
  1797. Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
  1798. return true;
  1799. if (Left.is(tok::comma))
  1800. return true;
  1801. if (Right.is(tok::comma))
  1802. return false;
  1803. if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
  1804. return true;
  1805. if (Right.is(TT_OverloadedOperatorLParen))
  1806. return false;
  1807. if (Right.is(tok::colon)) {
  1808. if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
  1809. !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
  1810. return false;
  1811. if (Right.is(TT_ObjCMethodExpr))
  1812. return false;
  1813. if (Left.is(tok::question))
  1814. return false;
  1815. if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
  1816. return false;
  1817. if (Right.is(TT_DictLiteral))
  1818. return Style.SpacesInContainerLiterals;
  1819. return true;
  1820. }
  1821. if (Left.is(TT_UnaryOperator))
  1822. return Right.is(TT_BinaryOperator);
  1823. // If the next token is a binary operator or a selector name, we have
  1824. // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
  1825. if (Left.is(TT_CastRParen))
  1826. return Style.SpaceAfterCStyleCast ||
  1827. Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
  1828. if (Left.is(tok::greater) && Right.is(tok::greater))
  1829. return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
  1830. (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
  1831. if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
  1832. Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
  1833. return false;
  1834. if (!Style.SpaceBeforeAssignmentOperators &&
  1835. Right.getPrecedence() == prec::Assignment)
  1836. return false;
  1837. if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
  1838. return (Left.is(TT_TemplateOpener) &&
  1839. Style.Standard == FormatStyle::LS_Cpp03) ||
  1840. !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
  1841. Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
  1842. if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
  1843. return Style.SpacesInAngles;
  1844. if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
  1845. Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr))
  1846. return true;
  1847. if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
  1848. Right.isNot(TT_FunctionTypeLParen))
  1849. return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
  1850. if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
  1851. Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
  1852. return false;
  1853. if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
  1854. Line.startsWith(tok::hash))
  1855. return true;
  1856. if (Right.is(TT_TrailingUnaryOperator))
  1857. return false;
  1858. if (Left.is(TT_RegexLiteral))
  1859. return false;
  1860. return spaceRequiredBetween(Line, Left, Right);
  1861. }
  1862. // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
  1863. static bool isAllmanBrace(const FormatToken &Tok) {
  1864. return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
  1865. !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
  1866. }
  1867. bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
  1868. const FormatToken &Right) {
  1869. const FormatToken &Left = *Right.Previous;
  1870. if (Right.NewlinesBefore > 1)
  1871. return true;
  1872. if (Style.Language == FormatStyle::LK_JavaScript) {
  1873. // FIXME: This might apply to other languages and token kinds.
  1874. if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
  1875. Left.Previous->is(tok::char_constant))
  1876. return true;
  1877. if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
  1878. Left.Previous && Left.Previous->is(tok::equal) &&
  1879. Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
  1880. tok::kw_const) &&
  1881. // kw_var is a pseudo-token that's a tok::identifier, so matches above.
  1882. !Line.startsWith(Keywords.kw_var))
  1883. // Object literals on the top level of a file are treated as "enum-style".
  1884. // Each key/value pair is put on a separate line, instead of bin-packing.
  1885. return true;
  1886. if (Left.is(tok::l_brace) && Line.Level == 0 &&
  1887. (Line.startsWith(tok::kw_enum) ||
  1888. Line.startsWith(tok::kw_export, tok::kw_enum)))
  1889. // JavaScript top-level enum key/value pairs are put on separate lines
  1890. // instead of bin-packing.
  1891. return true;
  1892. if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
  1893. !Left.Children.empty())
  1894. // Support AllowShortFunctionsOnASingleLine for JavaScript.
  1895. return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
  1896. (Left.NestingLevel == 0 && Line.Level == 0 &&
  1897. Style.AllowShortFunctionsOnASingleLine ==
  1898. FormatStyle::SFS_Inline);
  1899. } else if (Style.Language == FormatStyle::LK_Java) {
  1900. if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
  1901. Right.Next->is(tok::string_literal))
  1902. return true;
  1903. }
  1904. // If the last token before a '}' is a comma or a trailing comment, the
  1905. // intention is to insert a line break after it in order to make shuffling
  1906. // around entries easier.
  1907. const FormatToken *BeforeClosingBrace = nullptr;
  1908. if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
  1909. Left.BlockKind != BK_Block && Left.MatchingParen)
  1910. BeforeClosingBrace = Left.MatchingParen->Previous;
  1911. else if (Right.MatchingParen &&
  1912. Right.MatchingParen->isOneOf(tok::l_brace,
  1913. TT_ArrayInitializerLSquare))
  1914. BeforeClosingBrace = &Left;
  1915. if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
  1916. BeforeClosingBrace->isTrailingComment()))
  1917. return true;
  1918. if (Right.is(tok::comment))
  1919. return Left.BlockKind != BK_BracedInit &&
  1920. Left.isNot(TT_CtorInitializerColon) &&
  1921. (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
  1922. if (Left.isTrailingComment())
  1923. return true;
  1924. if (Left.isStringLiteral() &&
  1925. (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
  1926. return true;
  1927. if (Right.Previous->IsUnterminatedLiteral)
  1928. return true;
  1929. if (Right.is(tok::lessless) && Right.Next &&
  1930. Right.Previous->is(tok::string_literal) &&
  1931. Right.Next->is(tok::string_literal))
  1932. return true;
  1933. if (Right.Previous->ClosesTemplateDeclaration &&
  1934. Right.Previous->MatchingParen &&
  1935. Right.Previous->MatchingParen->NestingLevel == 0 &&
  1936. Style.AlwaysBreakTemplateDeclarations)
  1937. return true;
  1938. if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) &&
  1939. Style.BreakConstructorInitializersBeforeComma &&
  1940. !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
  1941. return true;
  1942. if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
  1943. // Raw string literals are special wrt. line breaks. The author has made a
  1944. // deliberate choice and might have aligned the contents of the string
  1945. // literal accordingly. Thus, we try keep existing line breaks.
  1946. return Right.NewlinesBefore > 0;
  1947. if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
  1948. Style.Language == FormatStyle::LK_Proto)
  1949. // Don't put enums onto single lines in protocol buffers.
  1950. return true;
  1951. if (Right.is(TT_InlineASMBrace))
  1952. return Right.HasUnescapedNewline;
  1953. if (isAllmanBrace(Left) || isAllmanBrace(Right))
  1954. return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
  1955. Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
  1956. (Style.BreakBeforeBraces == FormatStyle::BS_Mozilla &&
  1957. Line.startsWith(tok::kw_enum));
  1958. if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) &&
  1959. Right.is(TT_SelectorName))
  1960. return true;
  1961. if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
  1962. return true;
  1963. if ((Style.Language == FormatStyle::LK_Java ||
  1964. Style.Language == FormatStyle::LK_JavaScript) &&
  1965. Left.is(TT_LeadingJavaAnnotation) &&
  1966. Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
  1967. Line.Last->is(tok::l_brace))
  1968. return true;
  1969. return false;
  1970. }
  1971. bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
  1972. const FormatToken &Right) {
  1973. const FormatToken &Left = *Right.Previous;
  1974. // Language-specific stuff.
  1975. if (Style.Language == FormatStyle::LK_Java) {
  1976. if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
  1977. Keywords.kw_implements))
  1978. return false;
  1979. if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
  1980. Keywords.kw_implements))
  1981. return true;
  1982. } else if (Style.Language == FormatStyle::LK_JavaScript) {
  1983. if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
  1984. return false;
  1985. if (Left.is(TT_JsTypeColon))
  1986. return true;
  1987. }
  1988. if (Left.is(tok::at))
  1989. return false;
  1990. if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
  1991. return false;
  1992. if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
  1993. return !Right.is(tok::l_paren);
  1994. if (Right.is(TT_PointerOrReference))
  1995. return Line.IsMultiVariableDeclStmt ||
  1996. (Style.PointerAlignment == FormatStyle::PAS_Right &&
  1997. (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
  1998. if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
  1999. Right.is(tok::kw_operator))
  2000. return true;
  2001. if (Left.is(TT_PointerOrReference))
  2002. return false;
  2003. if (Right.isTrailingComment())
  2004. // We rely on MustBreakBefore being set correctly here as we should not
  2005. // change the "binding" behavior of a comment.
  2006. // The first comment in a braced lists is always interpreted as belonging to
  2007. // the first list element. Otherwise, it should be placed outside of the
  2008. // list.
  2009. return Left.BlockKind == BK_BracedInit;
  2010. if (Left.is(tok::question) && Right.is(tok::colon))
  2011. return false;
  2012. if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
  2013. return Style.BreakBeforeTernaryOperators;
  2014. if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
  2015. return !Style.BreakBeforeTernaryOperators;
  2016. if (Right.is(TT_InheritanceColon))
  2017. return true;
  2018. if (Right.is(tok::colon) &&
  2019. !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
  2020. return false;
  2021. if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
  2022. return true;
  2023. if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
  2024. Right.Next->is(TT_ObjCMethodExpr)))
  2025. return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
  2026. if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
  2027. return true;
  2028. if (Left.ClosesTemplateDeclaration)
  2029. return true;
  2030. if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
  2031. TT_OverloadedOperator))
  2032. return false;
  2033. if (Left.is(TT_RangeBasedForLoopColon))
  2034. return true;
  2035. if (Right.is(TT_RangeBasedForLoopColon))
  2036. return false;
  2037. if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
  2038. Left.is(tok::kw_operator))
  2039. return false;
  2040. if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
  2041. Line.Type == LT_VirtualFunctionDecl)
  2042. return false;
  2043. if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
  2044. return false;
  2045. if (Left.is(tok::l_paren) && Left.Previous &&
  2046. (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
  2047. return false;
  2048. if (Right.is(TT_ImplicitStringLiteral))
  2049. return false;
  2050. if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
  2051. return false;
  2052. if (Right.is(tok::r_square) && Right.MatchingParen &&
  2053. Right.MatchingParen->is(TT_LambdaLSquare))
  2054. return false;
  2055. // We only break before r_brace if there was a corresponding break before
  2056. // the l_brace, which is tracked by BreakBeforeClosingBrace.
  2057. if (Right.is(tok::r_brace))
  2058. return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
  2059. // Allow breaking after a trailing annotation, e.g. after a method
  2060. // declaration.
  2061. if (Left.is(TT_TrailingAnnotation))
  2062. return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
  2063. tok::less, tok::coloncolon);
  2064. if (Right.is(tok::kw___attribute))
  2065. return true;
  2066. if (Left.is(tok::identifier) && Right.is(tok::string_literal))
  2067. return true;
  2068. if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
  2069. return true;
  2070. if (Left.is(TT_CtorInitializerComma) &&
  2071. Style.BreakConstructorInitializersBeforeComma)
  2072. return false;
  2073. if (Right.is(TT_CtorInitializerComma) &&
  2074. Style.BreakConstructorInitializersBeforeComma)
  2075. return true;
  2076. if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
  2077. (Left.is(tok::less) && Right.is(tok::less)))
  2078. return false;
  2079. if (Right.is(TT_BinaryOperator) &&
  2080. Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
  2081. (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
  2082. Right.getPrecedence() != prec::Assignment))
  2083. return true;
  2084. if (Left.is(TT_ArrayInitializerLSquare))
  2085. return true;
  2086. if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
  2087. return true;
  2088. if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
  2089. !Left.isOneOf(tok::arrowstar, tok::lessless) &&
  2090. Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
  2091. (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
  2092. Left.getPrecedence() == prec::Assignment))
  2093. return true;
  2094. return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
  2095. tok::kw_class, tok::kw_struct) ||
  2096. Right.isMemberAccess() ||
  2097. Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
  2098. tok::colon, tok::l_square, tok::at) ||
  2099. (Left.is(tok::r_paren) &&
  2100. Right.isOneOf(tok::identifier, tok::kw_const)) ||
  2101. (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
  2102. }
  2103. void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
  2104. llvm::errs() << "AnnotatedTokens:\n";
  2105. const FormatToken *Tok = Line.First;
  2106. while (Tok) {
  2107. llvm::errs() << " M=" << Tok->MustBreakBefore
  2108. << " C=" << Tok->CanBreakBefore
  2109. << " T=" << getTokenTypeName(Tok->Type)
  2110. << " S=" << Tok->SpacesRequiredBefore
  2111. << " B=" << Tok->BlockParameterCount
  2112. << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
  2113. << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
  2114. << " FakeLParens=";
  2115. for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
  2116. llvm::errs() << Tok->FakeLParens[i] << "/";
  2117. llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
  2118. if (!Tok->Next)
  2119. assert(Tok == Line.Last);
  2120. Tok = Tok->Next;
  2121. }
  2122. llvm::errs() << "----\n";
  2123. }
  2124. } // namespace format
  2125. } // namespace clang