Format.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  1. //===--- Format.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 functions declared in Format.h. This will be
  12. /// split into separate files as we go.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "clang/Format/Format.h"
  16. #include "AffectedRangeManager.h"
  17. #include "ContinuationIndenter.h"
  18. #include "FormatInternal.h"
  19. #include "FormatTokenLexer.h"
  20. #include "NamespaceEndCommentsFixer.h"
  21. #include "SortJavaScriptImports.h"
  22. #include "TokenAnalyzer.h"
  23. #include "TokenAnnotator.h"
  24. #include "UnwrappedLineFormatter.h"
  25. #include "UnwrappedLineParser.h"
  26. #include "UsingDeclarationsSorter.h"
  27. #include "WhitespaceManager.h"
  28. #include "clang/Basic/Diagnostic.h"
  29. #include "clang/Basic/DiagnosticOptions.h"
  30. #include "clang/Basic/SourceManager.h"
  31. #include "clang/Basic/VirtualFileSystem.h"
  32. #include "clang/Lex/Lexer.h"
  33. #include "llvm/ADT/STLExtras.h"
  34. #include "llvm/Support/Allocator.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/Path.h"
  37. #include "llvm/Support/Regex.h"
  38. #include "llvm/Support/YAMLTraits.h"
  39. #include <algorithm>
  40. #include <memory>
  41. #include <string>
  42. #define DEBUG_TYPE "format-formatter"
  43. using clang::format::FormatStyle;
  44. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
  45. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::RawStringFormat)
  46. namespace llvm {
  47. namespace yaml {
  48. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
  49. static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
  50. IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
  51. IO.enumCase(Value, "Java", FormatStyle::LK_Java);
  52. IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
  53. IO.enumCase(Value, "ObjC", FormatStyle::LK_ObjC);
  54. IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
  55. IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen);
  56. IO.enumCase(Value, "TextProto", FormatStyle::LK_TextProto);
  57. }
  58. };
  59. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
  60. static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
  61. IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
  62. IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
  63. IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
  64. IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
  65. IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
  66. }
  67. };
  68. template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
  69. static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
  70. IO.enumCase(Value, "Never", FormatStyle::UT_Never);
  71. IO.enumCase(Value, "false", FormatStyle::UT_Never);
  72. IO.enumCase(Value, "Always", FormatStyle::UT_Always);
  73. IO.enumCase(Value, "true", FormatStyle::UT_Always);
  74. IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
  75. IO.enumCase(Value, "ForContinuationAndIndentation",
  76. FormatStyle::UT_ForContinuationAndIndentation);
  77. }
  78. };
  79. template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
  80. static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) {
  81. IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave);
  82. IO.enumCase(Value, "Single", FormatStyle::JSQS_Single);
  83. IO.enumCase(Value, "Double", FormatStyle::JSQS_Double);
  84. }
  85. };
  86. template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
  87. static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
  88. IO.enumCase(Value, "None", FormatStyle::SFS_None);
  89. IO.enumCase(Value, "false", FormatStyle::SFS_None);
  90. IO.enumCase(Value, "All", FormatStyle::SFS_All);
  91. IO.enumCase(Value, "true", FormatStyle::SFS_All);
  92. IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
  93. IO.enumCase(Value, "InlineOnly", FormatStyle::SFS_InlineOnly);
  94. IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
  95. }
  96. };
  97. template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
  98. static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
  99. IO.enumCase(Value, "All", FormatStyle::BOS_All);
  100. IO.enumCase(Value, "true", FormatStyle::BOS_All);
  101. IO.enumCase(Value, "None", FormatStyle::BOS_None);
  102. IO.enumCase(Value, "false", FormatStyle::BOS_None);
  103. IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
  104. }
  105. };
  106. template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
  107. static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
  108. IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
  109. IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
  110. IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
  111. IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
  112. IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
  113. IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
  114. IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
  115. IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
  116. }
  117. };
  118. template <>
  119. struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> {
  120. static void
  121. enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) {
  122. IO.enumCase(Value, "BeforeColon", FormatStyle::BCIS_BeforeColon);
  123. IO.enumCase(Value, "BeforeComma", FormatStyle::BCIS_BeforeComma);
  124. IO.enumCase(Value, "AfterColon", FormatStyle::BCIS_AfterColon);
  125. }
  126. };
  127. template <>
  128. struct ScalarEnumerationTraits<FormatStyle::PPDirectiveIndentStyle> {
  129. static void enumeration(IO &IO, FormatStyle::PPDirectiveIndentStyle &Value) {
  130. IO.enumCase(Value, "None", FormatStyle::PPDIS_None);
  131. IO.enumCase(Value, "AfterHash", FormatStyle::PPDIS_AfterHash);
  132. }
  133. };
  134. template <>
  135. struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> {
  136. static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) {
  137. IO.enumCase(Value, "None", FormatStyle::RTBS_None);
  138. IO.enumCase(Value, "All", FormatStyle::RTBS_All);
  139. IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel);
  140. IO.enumCase(Value, "TopLevelDefinitions",
  141. FormatStyle::RTBS_TopLevelDefinitions);
  142. IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions);
  143. }
  144. };
  145. template <>
  146. struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
  147. static void
  148. enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
  149. IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
  150. IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
  151. IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
  152. // For backward compatibility.
  153. IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
  154. IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
  155. }
  156. };
  157. template <>
  158. struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
  159. static void enumeration(IO &IO,
  160. FormatStyle::NamespaceIndentationKind &Value) {
  161. IO.enumCase(Value, "None", FormatStyle::NI_None);
  162. IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
  163. IO.enumCase(Value, "All", FormatStyle::NI_All);
  164. }
  165. };
  166. template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
  167. static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
  168. IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
  169. IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
  170. IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
  171. // For backward compatibility.
  172. IO.enumCase(Value, "true", FormatStyle::BAS_Align);
  173. IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
  174. }
  175. };
  176. template <>
  177. struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
  178. static void enumeration(IO &IO,
  179. FormatStyle::EscapedNewlineAlignmentStyle &Value) {
  180. IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
  181. IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
  182. IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
  183. // For backward compatibility.
  184. IO.enumCase(Value, "true", FormatStyle::ENAS_Left);
  185. IO.enumCase(Value, "false", FormatStyle::ENAS_Right);
  186. }
  187. };
  188. template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
  189. static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
  190. IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
  191. IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
  192. IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
  193. // For backward compatibility.
  194. IO.enumCase(Value, "true", FormatStyle::PAS_Left);
  195. IO.enumCase(Value, "false", FormatStyle::PAS_Right);
  196. }
  197. };
  198. template <>
  199. struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
  200. static void enumeration(IO &IO,
  201. FormatStyle::SpaceBeforeParensOptions &Value) {
  202. IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
  203. IO.enumCase(Value, "ControlStatements",
  204. FormatStyle::SBPO_ControlStatements);
  205. IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
  206. // For backward compatibility.
  207. IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
  208. IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
  209. }
  210. };
  211. template <> struct MappingTraits<FormatStyle> {
  212. static void mapping(IO &IO, FormatStyle &Style) {
  213. // When reading, read the language first, we need it for getPredefinedStyle.
  214. IO.mapOptional("Language", Style.Language);
  215. if (IO.outputting()) {
  216. StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
  217. "Mozilla", "WebKit", "GNU"};
  218. ArrayRef<StringRef> Styles(StylesArray);
  219. for (size_t i = 0, e = Styles.size(); i < e; ++i) {
  220. StringRef StyleName(Styles[i]);
  221. FormatStyle PredefinedStyle;
  222. if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
  223. Style == PredefinedStyle) {
  224. IO.mapOptional("# BasedOnStyle", StyleName);
  225. break;
  226. }
  227. }
  228. } else {
  229. StringRef BasedOnStyle;
  230. IO.mapOptional("BasedOnStyle", BasedOnStyle);
  231. if (!BasedOnStyle.empty()) {
  232. FormatStyle::LanguageKind OldLanguage = Style.Language;
  233. FormatStyle::LanguageKind Language =
  234. ((FormatStyle *)IO.getContext())->Language;
  235. if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
  236. IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
  237. return;
  238. }
  239. Style.Language = OldLanguage;
  240. }
  241. }
  242. // For backward compatibility.
  243. if (!IO.outputting()) {
  244. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
  245. IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
  246. IO.mapOptional("IndentFunctionDeclarationAfterType",
  247. Style.IndentWrappedFunctionNames);
  248. IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
  249. IO.mapOptional("SpaceAfterControlStatementKeyword",
  250. Style.SpaceBeforeParens);
  251. }
  252. IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
  253. IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
  254. IO.mapOptional("AlignConsecutiveAssignments",
  255. Style.AlignConsecutiveAssignments);
  256. IO.mapOptional("AlignConsecutiveDeclarations",
  257. Style.AlignConsecutiveDeclarations);
  258. IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
  259. IO.mapOptional("AlignOperands", Style.AlignOperands);
  260. IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
  261. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  262. Style.AllowAllParametersOfDeclarationOnNextLine);
  263. IO.mapOptional("AllowShortBlocksOnASingleLine",
  264. Style.AllowShortBlocksOnASingleLine);
  265. IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
  266. Style.AllowShortCaseLabelsOnASingleLine);
  267. IO.mapOptional("AllowShortFunctionsOnASingleLine",
  268. Style.AllowShortFunctionsOnASingleLine);
  269. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  270. Style.AllowShortIfStatementsOnASingleLine);
  271. IO.mapOptional("AllowShortLoopsOnASingleLine",
  272. Style.AllowShortLoopsOnASingleLine);
  273. IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
  274. Style.AlwaysBreakAfterDefinitionReturnType);
  275. IO.mapOptional("AlwaysBreakAfterReturnType",
  276. Style.AlwaysBreakAfterReturnType);
  277. // If AlwaysBreakAfterDefinitionReturnType was specified but
  278. // AlwaysBreakAfterReturnType was not, initialize the latter from the
  279. // former for backwards compatibility.
  280. if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None &&
  281. Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) {
  282. if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All)
  283. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  284. else if (Style.AlwaysBreakAfterDefinitionReturnType ==
  285. FormatStyle::DRTBS_TopLevel)
  286. Style.AlwaysBreakAfterReturnType =
  287. FormatStyle::RTBS_TopLevelDefinitions;
  288. }
  289. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  290. Style.AlwaysBreakBeforeMultilineStrings);
  291. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  292. Style.AlwaysBreakTemplateDeclarations);
  293. IO.mapOptional("BinPackArguments", Style.BinPackArguments);
  294. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  295. IO.mapOptional("BraceWrapping", Style.BraceWrapping);
  296. IO.mapOptional("BreakBeforeBinaryOperators",
  297. Style.BreakBeforeBinaryOperators);
  298. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  299. IO.mapOptional("BreakBeforeInheritanceComma",
  300. Style.BreakBeforeInheritanceComma);
  301. IO.mapOptional("BreakBeforeTernaryOperators",
  302. Style.BreakBeforeTernaryOperators);
  303. bool BreakConstructorInitializersBeforeComma = false;
  304. IO.mapOptional("BreakConstructorInitializersBeforeComma",
  305. BreakConstructorInitializersBeforeComma);
  306. IO.mapOptional("BreakConstructorInitializers",
  307. Style.BreakConstructorInitializers);
  308. // If BreakConstructorInitializersBeforeComma was specified but
  309. // BreakConstructorInitializers was not, initialize the latter from the
  310. // former for backwards compatibility.
  311. if (BreakConstructorInitializersBeforeComma &&
  312. Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon)
  313. Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
  314. IO.mapOptional("BreakAfterJavaFieldAnnotations",
  315. Style.BreakAfterJavaFieldAnnotations);
  316. IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
  317. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  318. IO.mapOptional("CommentPragmas", Style.CommentPragmas);
  319. IO.mapOptional("CompactNamespaces", Style.CompactNamespaces);
  320. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  321. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  322. IO.mapOptional("ConstructorInitializerIndentWidth",
  323. Style.ConstructorInitializerIndentWidth);
  324. IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
  325. IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
  326. IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
  327. IO.mapOptional("DisableFormat", Style.DisableFormat);
  328. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  329. Style.ExperimentalAutoDetectBinPacking);
  330. IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
  331. IO.mapOptional("ForEachMacros", Style.ForEachMacros);
  332. IO.mapOptional("IncludeBlocks", Style.IncludeBlocks);
  333. IO.mapOptional("IncludeCategories", Style.IncludeCategories);
  334. IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
  335. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  336. IO.mapOptional("IndentPPDirectives", Style.IndentPPDirectives);
  337. IO.mapOptional("IndentWidth", Style.IndentWidth);
  338. IO.mapOptional("IndentWrappedFunctionNames",
  339. Style.IndentWrappedFunctionNames);
  340. IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes);
  341. IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports);
  342. IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
  343. Style.KeepEmptyLinesAtTheStartOfBlocks);
  344. IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
  345. IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
  346. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  347. IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
  348. IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
  349. IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
  350. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  351. Style.ObjCSpaceBeforeProtocolList);
  352. IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
  353. IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
  354. Style.PenaltyBreakBeforeFirstCallParameter);
  355. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  356. IO.mapOptional("PenaltyBreakFirstLessLess",
  357. Style.PenaltyBreakFirstLessLess);
  358. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  359. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  360. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  361. Style.PenaltyReturnTypeOnItsOwnLine);
  362. IO.mapOptional("PointerAlignment", Style.PointerAlignment);
  363. IO.mapOptional("RawStringFormats", Style.RawStringFormats);
  364. IO.mapOptional("ReflowComments", Style.ReflowComments);
  365. IO.mapOptional("SortIncludes", Style.SortIncludes);
  366. IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
  367. IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
  368. IO.mapOptional("SpaceAfterTemplateKeyword",
  369. Style.SpaceAfterTemplateKeyword);
  370. IO.mapOptional("SpaceBeforeAssignmentOperators",
  371. Style.SpaceBeforeAssignmentOperators);
  372. IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
  373. IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
  374. IO.mapOptional("SpacesBeforeTrailingComments",
  375. Style.SpacesBeforeTrailingComments);
  376. IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
  377. IO.mapOptional("SpacesInContainerLiterals",
  378. Style.SpacesInContainerLiterals);
  379. IO.mapOptional("SpacesInCStyleCastParentheses",
  380. Style.SpacesInCStyleCastParentheses);
  381. IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
  382. IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
  383. IO.mapOptional("Standard", Style.Standard);
  384. IO.mapOptional("TabWidth", Style.TabWidth);
  385. IO.mapOptional("UseTab", Style.UseTab);
  386. }
  387. };
  388. template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
  389. static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
  390. IO.mapOptional("AfterClass", Wrapping.AfterClass);
  391. IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
  392. IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
  393. IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
  394. IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
  395. IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
  396. IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
  397. IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
  398. IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
  399. IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
  400. IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
  401. IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
  402. IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
  403. IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord);
  404. IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace);
  405. }
  406. };
  407. template <> struct MappingTraits<FormatStyle::IncludeCategory> {
  408. static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
  409. IO.mapOptional("Regex", Category.Regex);
  410. IO.mapOptional("Priority", Category.Priority);
  411. }
  412. };
  413. template <> struct ScalarEnumerationTraits<FormatStyle::IncludeBlocksStyle> {
  414. static void enumeration(IO &IO, FormatStyle::IncludeBlocksStyle &Value) {
  415. IO.enumCase(Value, "Preserve", FormatStyle::IBS_Preserve);
  416. IO.enumCase(Value, "Merge", FormatStyle::IBS_Merge);
  417. IO.enumCase(Value, "Regroup", FormatStyle::IBS_Regroup);
  418. }
  419. };
  420. template <> struct MappingTraits<FormatStyle::RawStringFormat> {
  421. static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
  422. IO.mapOptional("Delimiter", Format.Delimiter);
  423. IO.mapOptional("Language", Format.Language);
  424. IO.mapOptional("BasedOnStyle", Format.BasedOnStyle);
  425. }
  426. };
  427. // Allows to read vector<FormatStyle> while keeping default values.
  428. // IO.getContext() should contain a pointer to the FormatStyle structure, that
  429. // will be used to get default values for missing keys.
  430. // If the first element has no Language specified, it will be treated as the
  431. // default one for the following elements.
  432. template <> struct DocumentListTraits<std::vector<FormatStyle>> {
  433. static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
  434. return Seq.size();
  435. }
  436. static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
  437. size_t Index) {
  438. if (Index >= Seq.size()) {
  439. assert(Index == Seq.size());
  440. FormatStyle Template;
  441. if (!Seq.empty() && Seq[0].Language == FormatStyle::LK_None) {
  442. Template = Seq[0];
  443. } else {
  444. Template = *((const FormatStyle *)IO.getContext());
  445. Template.Language = FormatStyle::LK_None;
  446. }
  447. Seq.resize(Index + 1, Template);
  448. }
  449. return Seq[Index];
  450. }
  451. };
  452. } // namespace yaml
  453. } // namespace llvm
  454. namespace clang {
  455. namespace format {
  456. const std::error_category &getParseCategory() {
  457. static ParseErrorCategory C;
  458. return C;
  459. }
  460. std::error_code make_error_code(ParseError e) {
  461. return std::error_code(static_cast<int>(e), getParseCategory());
  462. }
  463. inline llvm::Error make_string_error(const llvm::Twine &Message) {
  464. return llvm::make_error<llvm::StringError>(Message,
  465. llvm::inconvertibleErrorCode());
  466. }
  467. const char *ParseErrorCategory::name() const noexcept {
  468. return "clang-format.parse_error";
  469. }
  470. std::string ParseErrorCategory::message(int EV) const {
  471. switch (static_cast<ParseError>(EV)) {
  472. case ParseError::Success:
  473. return "Success";
  474. case ParseError::Error:
  475. return "Invalid argument";
  476. case ParseError::Unsuitable:
  477. return "Unsuitable";
  478. }
  479. llvm_unreachable("unexpected parse error");
  480. }
  481. static FormatStyle expandPresets(const FormatStyle &Style) {
  482. if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
  483. return Style;
  484. FormatStyle Expanded = Style;
  485. Expanded.BraceWrapping = {false, false, false, false, false,
  486. false, false, false, false, false,
  487. false, false, true, true, true};
  488. switch (Style.BreakBeforeBraces) {
  489. case FormatStyle::BS_Linux:
  490. Expanded.BraceWrapping.AfterClass = true;
  491. Expanded.BraceWrapping.AfterFunction = true;
  492. Expanded.BraceWrapping.AfterNamespace = true;
  493. break;
  494. case FormatStyle::BS_Mozilla:
  495. Expanded.BraceWrapping.AfterClass = true;
  496. Expanded.BraceWrapping.AfterEnum = true;
  497. Expanded.BraceWrapping.AfterFunction = true;
  498. Expanded.BraceWrapping.AfterStruct = true;
  499. Expanded.BraceWrapping.AfterUnion = true;
  500. Expanded.BraceWrapping.AfterExternBlock = true;
  501. Expanded.BraceWrapping.SplitEmptyFunction = true;
  502. Expanded.BraceWrapping.SplitEmptyRecord = false;
  503. break;
  504. case FormatStyle::BS_Stroustrup:
  505. Expanded.BraceWrapping.AfterFunction = true;
  506. Expanded.BraceWrapping.BeforeCatch = true;
  507. Expanded.BraceWrapping.BeforeElse = true;
  508. break;
  509. case FormatStyle::BS_Allman:
  510. Expanded.BraceWrapping.AfterClass = true;
  511. Expanded.BraceWrapping.AfterControlStatement = true;
  512. Expanded.BraceWrapping.AfterEnum = true;
  513. Expanded.BraceWrapping.AfterFunction = true;
  514. Expanded.BraceWrapping.AfterNamespace = true;
  515. Expanded.BraceWrapping.AfterObjCDeclaration = true;
  516. Expanded.BraceWrapping.AfterStruct = true;
  517. Expanded.BraceWrapping.AfterExternBlock = true;
  518. Expanded.BraceWrapping.BeforeCatch = true;
  519. Expanded.BraceWrapping.BeforeElse = true;
  520. break;
  521. case FormatStyle::BS_GNU:
  522. Expanded.BraceWrapping = {true, true, true, true, true, true, true, true,
  523. true, true, true, true, true, true, true};
  524. break;
  525. case FormatStyle::BS_WebKit:
  526. Expanded.BraceWrapping.AfterFunction = true;
  527. break;
  528. default:
  529. break;
  530. }
  531. return Expanded;
  532. }
  533. FormatStyle getLLVMStyle() {
  534. FormatStyle LLVMStyle;
  535. LLVMStyle.Language = FormatStyle::LK_Cpp;
  536. LLVMStyle.AccessModifierOffset = -2;
  537. LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
  538. LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  539. LLVMStyle.AlignOperands = true;
  540. LLVMStyle.AlignTrailingComments = true;
  541. LLVMStyle.AlignConsecutiveAssignments = false;
  542. LLVMStyle.AlignConsecutiveDeclarations = false;
  543. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  544. LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  545. LLVMStyle.AllowShortBlocksOnASingleLine = false;
  546. LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
  547. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  548. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  549. LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  550. LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
  551. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  552. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  553. LLVMStyle.BinPackArguments = true;
  554. LLVMStyle.BinPackParameters = true;
  555. LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  556. LLVMStyle.BreakBeforeTernaryOperators = true;
  557. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  558. LLVMStyle.BraceWrapping = {false, false, false, false, false,
  559. false, false, false, false, false,
  560. false, false, true, true, true};
  561. LLVMStyle.BreakAfterJavaFieldAnnotations = false;
  562. LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
  563. LLVMStyle.BreakBeforeInheritanceComma = false;
  564. LLVMStyle.BreakStringLiterals = true;
  565. LLVMStyle.ColumnLimit = 80;
  566. LLVMStyle.CommentPragmas = "^ IWYU pragma:";
  567. LLVMStyle.CompactNamespaces = false;
  568. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  569. LLVMStyle.ConstructorInitializerIndentWidth = 4;
  570. LLVMStyle.ContinuationIndentWidth = 4;
  571. LLVMStyle.Cpp11BracedListStyle = true;
  572. LLVMStyle.DerivePointerAlignment = false;
  573. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  574. LLVMStyle.FixNamespaceComments = true;
  575. LLVMStyle.ForEachMacros.push_back("foreach");
  576. LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
  577. LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
  578. LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
  579. {"^(<|\"(gtest|gmock|isl|json)/)", 3},
  580. {".*", 1}};
  581. LLVMStyle.IncludeIsMainRegex = "(Test)?$";
  582. LLVMStyle.IncludeBlocks = FormatStyle::IBS_Preserve;
  583. LLVMStyle.IndentCaseLabels = false;
  584. LLVMStyle.IndentPPDirectives = FormatStyle::PPDIS_None;
  585. LLVMStyle.IndentWrappedFunctionNames = false;
  586. LLVMStyle.IndentWidth = 2;
  587. LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  588. LLVMStyle.JavaScriptWrapImports = true;
  589. LLVMStyle.TabWidth = 8;
  590. LLVMStyle.MaxEmptyLinesToKeep = 1;
  591. LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
  592. LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
  593. LLVMStyle.ObjCBlockIndentWidth = 2;
  594. LLVMStyle.ObjCSpaceAfterProperty = false;
  595. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  596. LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
  597. LLVMStyle.SpacesBeforeTrailingComments = 1;
  598. LLVMStyle.Standard = FormatStyle::LS_Cpp11;
  599. LLVMStyle.UseTab = FormatStyle::UT_Never;
  600. LLVMStyle.RawStringFormats = {{"pb", FormatStyle::LK_TextProto, "google"}};
  601. LLVMStyle.ReflowComments = true;
  602. LLVMStyle.SpacesInParentheses = false;
  603. LLVMStyle.SpacesInSquareBrackets = false;
  604. LLVMStyle.SpaceInEmptyParentheses = false;
  605. LLVMStyle.SpacesInContainerLiterals = true;
  606. LLVMStyle.SpacesInCStyleCastParentheses = false;
  607. LLVMStyle.SpaceAfterCStyleCast = false;
  608. LLVMStyle.SpaceAfterTemplateKeyword = true;
  609. LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
  610. LLVMStyle.SpaceBeforeAssignmentOperators = true;
  611. LLVMStyle.SpacesInAngles = false;
  612. LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
  613. LLVMStyle.PenaltyBreakComment = 300;
  614. LLVMStyle.PenaltyBreakFirstLessLess = 120;
  615. LLVMStyle.PenaltyBreakString = 1000;
  616. LLVMStyle.PenaltyExcessCharacter = 1000000;
  617. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  618. LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
  619. LLVMStyle.DisableFormat = false;
  620. LLVMStyle.SortIncludes = true;
  621. LLVMStyle.SortUsingDeclarations = true;
  622. return LLVMStyle;
  623. }
  624. FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
  625. if (Language == FormatStyle::LK_TextProto) {
  626. FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_Proto);
  627. GoogleStyle.Language = FormatStyle::LK_TextProto;
  628. return GoogleStyle;
  629. }
  630. FormatStyle GoogleStyle = getLLVMStyle();
  631. GoogleStyle.Language = Language;
  632. GoogleStyle.AccessModifierOffset = -1;
  633. GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left;
  634. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  635. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  636. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  637. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  638. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  639. GoogleStyle.DerivePointerAlignment = true;
  640. GoogleStyle.IncludeCategories = {
  641. {"^<ext/.*\\.h>", 2}, {"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
  642. GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
  643. GoogleStyle.IndentCaseLabels = true;
  644. GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
  645. GoogleStyle.ObjCSpaceAfterProperty = false;
  646. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  647. GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
  648. GoogleStyle.SpacesBeforeTrailingComments = 2;
  649. GoogleStyle.Standard = FormatStyle::LS_Auto;
  650. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  651. GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
  652. if (Language == FormatStyle::LK_Java) {
  653. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  654. GoogleStyle.AlignOperands = false;
  655. GoogleStyle.AlignTrailingComments = false;
  656. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  657. GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
  658. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  659. GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  660. GoogleStyle.ColumnLimit = 100;
  661. GoogleStyle.SpaceAfterCStyleCast = true;
  662. GoogleStyle.SpacesBeforeTrailingComments = 1;
  663. } else if (Language == FormatStyle::LK_JavaScript) {
  664. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  665. GoogleStyle.AlignOperands = false;
  666. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  667. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  668. GoogleStyle.BreakBeforeTernaryOperators = false;
  669. // taze:, triple slash directives (`/// <...`), @tag followed by { for a lot
  670. // of JSDoc tags, and @see, which is commonly followed by overlong URLs.
  671. GoogleStyle.CommentPragmas =
  672. "(taze:|^/[ \t]*<|(@[A-Za-z_0-9-]+[ \\t]*{)|@see)";
  673. GoogleStyle.MaxEmptyLinesToKeep = 3;
  674. GoogleStyle.NamespaceIndentation = FormatStyle::NI_All;
  675. GoogleStyle.SpacesInContainerLiterals = false;
  676. GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single;
  677. GoogleStyle.JavaScriptWrapImports = false;
  678. } else if (Language == FormatStyle::LK_Proto) {
  679. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  680. GoogleStyle.SpacesInContainerLiterals = false;
  681. } else if (Language == FormatStyle::LK_ObjC) {
  682. GoogleStyle.ColumnLimit = 100;
  683. }
  684. return GoogleStyle;
  685. }
  686. FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
  687. FormatStyle ChromiumStyle = getGoogleStyle(Language);
  688. if (Language == FormatStyle::LK_Java) {
  689. ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
  690. ChromiumStyle.BreakAfterJavaFieldAnnotations = true;
  691. ChromiumStyle.ContinuationIndentWidth = 8;
  692. ChromiumStyle.IndentWidth = 4;
  693. } else if (Language == FormatStyle::LK_JavaScript) {
  694. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  695. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  696. } else {
  697. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  698. ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  699. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  700. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  701. ChromiumStyle.BinPackParameters = false;
  702. ChromiumStyle.DerivePointerAlignment = false;
  703. if (Language == FormatStyle::LK_ObjC)
  704. ChromiumStyle.ColumnLimit = 80;
  705. }
  706. return ChromiumStyle;
  707. }
  708. FormatStyle getMozillaStyle() {
  709. FormatStyle MozillaStyle = getLLVMStyle();
  710. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  711. MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  712. MozillaStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
  713. MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
  714. FormatStyle::DRTBS_TopLevel;
  715. MozillaStyle.AlwaysBreakTemplateDeclarations = true;
  716. MozillaStyle.BinPackParameters = false;
  717. MozillaStyle.BinPackArguments = false;
  718. MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  719. MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
  720. MozillaStyle.BreakBeforeInheritanceComma = true;
  721. MozillaStyle.ConstructorInitializerIndentWidth = 2;
  722. MozillaStyle.ContinuationIndentWidth = 2;
  723. MozillaStyle.Cpp11BracedListStyle = false;
  724. MozillaStyle.FixNamespaceComments = false;
  725. MozillaStyle.IndentCaseLabels = true;
  726. MozillaStyle.ObjCSpaceAfterProperty = true;
  727. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  728. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  729. MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
  730. MozillaStyle.SpaceAfterTemplateKeyword = false;
  731. return MozillaStyle;
  732. }
  733. FormatStyle getWebKitStyle() {
  734. FormatStyle Style = getLLVMStyle();
  735. Style.AccessModifierOffset = -4;
  736. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  737. Style.AlignOperands = false;
  738. Style.AlignTrailingComments = false;
  739. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  740. Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
  741. Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
  742. Style.Cpp11BracedListStyle = false;
  743. Style.ColumnLimit = 0;
  744. Style.FixNamespaceComments = false;
  745. Style.IndentWidth = 4;
  746. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  747. Style.ObjCBlockIndentWidth = 4;
  748. Style.ObjCSpaceAfterProperty = true;
  749. Style.PointerAlignment = FormatStyle::PAS_Left;
  750. return Style;
  751. }
  752. FormatStyle getGNUStyle() {
  753. FormatStyle Style = getLLVMStyle();
  754. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  755. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  756. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  757. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  758. Style.BreakBeforeTernaryOperators = true;
  759. Style.Cpp11BracedListStyle = false;
  760. Style.ColumnLimit = 79;
  761. Style.FixNamespaceComments = false;
  762. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  763. Style.Standard = FormatStyle::LS_Cpp03;
  764. return Style;
  765. }
  766. FormatStyle getNoStyle() {
  767. FormatStyle NoStyle = getLLVMStyle();
  768. NoStyle.DisableFormat = true;
  769. NoStyle.SortIncludes = false;
  770. NoStyle.SortUsingDeclarations = false;
  771. return NoStyle;
  772. }
  773. bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
  774. FormatStyle *Style) {
  775. if (Name.equals_lower("llvm")) {
  776. *Style = getLLVMStyle();
  777. } else if (Name.equals_lower("chromium")) {
  778. *Style = getChromiumStyle(Language);
  779. } else if (Name.equals_lower("mozilla")) {
  780. *Style = getMozillaStyle();
  781. } else if (Name.equals_lower("google")) {
  782. *Style = getGoogleStyle(Language);
  783. } else if (Name.equals_lower("webkit")) {
  784. *Style = getWebKitStyle();
  785. } else if (Name.equals_lower("gnu")) {
  786. *Style = getGNUStyle();
  787. } else if (Name.equals_lower("none")) {
  788. *Style = getNoStyle();
  789. } else {
  790. return false;
  791. }
  792. Style->Language = Language;
  793. return true;
  794. }
  795. std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  796. assert(Style);
  797. FormatStyle::LanguageKind Language = Style->Language;
  798. assert(Language != FormatStyle::LK_None);
  799. if (Text.trim().empty())
  800. return make_error_code(ParseError::Error);
  801. std::vector<FormatStyle> Styles;
  802. llvm::yaml::Input Input(Text);
  803. // DocumentListTraits<vector<FormatStyle>> uses the context to get default
  804. // values for the fields, keys for which are missing from the configuration.
  805. // Mapping also uses the context to get the language to find the correct
  806. // base style.
  807. Input.setContext(Style);
  808. Input >> Styles;
  809. if (Input.error())
  810. return Input.error();
  811. for (unsigned i = 0; i < Styles.size(); ++i) {
  812. // Ensures that only the first configuration can skip the Language option.
  813. if (Styles[i].Language == FormatStyle::LK_None && i != 0)
  814. return make_error_code(ParseError::Error);
  815. // Ensure that each language is configured at most once.
  816. for (unsigned j = 0; j < i; ++j) {
  817. if (Styles[i].Language == Styles[j].Language) {
  818. DEBUG(llvm::dbgs()
  819. << "Duplicate languages in the config file on positions " << j
  820. << " and " << i << "\n");
  821. return make_error_code(ParseError::Error);
  822. }
  823. }
  824. }
  825. // Look for a suitable configuration starting from the end, so we can
  826. // find the configuration for the specific language first, and the default
  827. // configuration (which can only be at slot 0) after it.
  828. for (int i = Styles.size() - 1; i >= 0; --i) {
  829. if (Styles[i].Language == Language ||
  830. Styles[i].Language == FormatStyle::LK_None) {
  831. *Style = Styles[i];
  832. Style->Language = Language;
  833. return make_error_code(ParseError::Success);
  834. }
  835. }
  836. return make_error_code(ParseError::Unsuitable);
  837. }
  838. std::string configurationAsText(const FormatStyle &Style) {
  839. std::string Text;
  840. llvm::raw_string_ostream Stream(Text);
  841. llvm::yaml::Output Output(Stream);
  842. // We use the same mapping method for input and output, so we need a non-const
  843. // reference here.
  844. FormatStyle NonConstStyle = expandPresets(Style);
  845. Output << NonConstStyle;
  846. return Stream.str();
  847. }
  848. namespace {
  849. class JavaScriptRequoter : public TokenAnalyzer {
  850. public:
  851. JavaScriptRequoter(const Environment &Env, const FormatStyle &Style)
  852. : TokenAnalyzer(Env, Style) {}
  853. std::pair<tooling::Replacements, unsigned>
  854. analyze(TokenAnnotator &Annotator,
  855. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  856. FormatTokenLexer &Tokens) override {
  857. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  858. AnnotatedLines.end());
  859. tooling::Replacements Result;
  860. requoteJSStringLiteral(AnnotatedLines, Result);
  861. return {Result, 0};
  862. }
  863. private:
  864. // Replaces double/single-quoted string literal as appropriate, re-escaping
  865. // the contents in the process.
  866. void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines,
  867. tooling::Replacements &Result) {
  868. for (AnnotatedLine *Line : Lines) {
  869. requoteJSStringLiteral(Line->Children, Result);
  870. if (!Line->Affected)
  871. continue;
  872. for (FormatToken *FormatTok = Line->First; FormatTok;
  873. FormatTok = FormatTok->Next) {
  874. StringRef Input = FormatTok->TokenText;
  875. if (FormatTok->Finalized || !FormatTok->isStringLiteral() ||
  876. // NB: testing for not starting with a double quote to avoid
  877. // breaking `template strings`.
  878. (Style.JavaScriptQuotes == FormatStyle::JSQS_Single &&
  879. !Input.startswith("\"")) ||
  880. (Style.JavaScriptQuotes == FormatStyle::JSQS_Double &&
  881. !Input.startswith("\'")))
  882. continue;
  883. // Change start and end quote.
  884. bool IsSingle = Style.JavaScriptQuotes == FormatStyle::JSQS_Single;
  885. SourceLocation Start = FormatTok->Tok.getLocation();
  886. auto Replace = [&](SourceLocation Start, unsigned Length,
  887. StringRef ReplacementText) {
  888. auto Err = Result.add(tooling::Replacement(
  889. Env.getSourceManager(), Start, Length, ReplacementText));
  890. // FIXME: handle error. For now, print error message and skip the
  891. // replacement for release version.
  892. if (Err) {
  893. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  894. assert(false);
  895. }
  896. };
  897. Replace(Start, 1, IsSingle ? "'" : "\"");
  898. Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1,
  899. IsSingle ? "'" : "\"");
  900. // Escape internal quotes.
  901. bool Escaped = false;
  902. for (size_t i = 1; i < Input.size() - 1; i++) {
  903. switch (Input[i]) {
  904. case '\\':
  905. if (!Escaped && i + 1 < Input.size() &&
  906. ((IsSingle && Input[i + 1] == '"') ||
  907. (!IsSingle && Input[i + 1] == '\''))) {
  908. // Remove this \, it's escaping a " or ' that no longer needs
  909. // escaping
  910. Replace(Start.getLocWithOffset(i), 1, "");
  911. continue;
  912. }
  913. Escaped = !Escaped;
  914. break;
  915. case '\"':
  916. case '\'':
  917. if (!Escaped && IsSingle == (Input[i] == '\'')) {
  918. // Escape the quote.
  919. Replace(Start.getLocWithOffset(i), 0, "\\");
  920. }
  921. Escaped = false;
  922. break;
  923. default:
  924. Escaped = false;
  925. break;
  926. }
  927. }
  928. }
  929. }
  930. }
  931. };
  932. class Formatter : public TokenAnalyzer {
  933. public:
  934. Formatter(const Environment &Env, const FormatStyle &Style,
  935. FormattingAttemptStatus *Status)
  936. : TokenAnalyzer(Env, Style), Status(Status) {}
  937. std::pair<tooling::Replacements, unsigned>
  938. analyze(TokenAnnotator &Annotator,
  939. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  940. FormatTokenLexer &Tokens) override {
  941. tooling::Replacements Result;
  942. deriveLocalStyle(AnnotatedLines);
  943. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  944. AnnotatedLines.end());
  945. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  946. Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
  947. }
  948. Annotator.setCommentLineLevels(AnnotatedLines);
  949. WhitespaceManager Whitespaces(
  950. Env.getSourceManager(), Style,
  951. inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID())));
  952. ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
  953. Env.getSourceManager(), Whitespaces, Encoding,
  954. BinPackInconclusiveFunctions);
  955. unsigned Penalty =
  956. UnwrappedLineFormatter(&Indenter, &Whitespaces, Style,
  957. Tokens.getKeywords(), Env.getSourceManager(),
  958. Status)
  959. .format(AnnotatedLines, /*DryRun=*/false,
  960. /*AdditionalIndent=*/0,
  961. /*FixBadIndentation=*/false,
  962. /*FirstStartColumn=*/Env.getFirstStartColumn(),
  963. /*NextStartColumn=*/Env.getNextStartColumn(),
  964. /*LastStartColumn=*/Env.getLastStartColumn());
  965. for (const auto &R : Whitespaces.generateReplacements())
  966. if (Result.add(R))
  967. return std::make_pair(Result, 0);
  968. return std::make_pair(Result, Penalty);
  969. }
  970. private:
  971. static bool inputUsesCRLF(StringRef Text) {
  972. return Text.count('\r') * 2 > Text.count('\n');
  973. }
  974. bool
  975. hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  976. for (const AnnotatedLine *Line : Lines) {
  977. if (hasCpp03IncompatibleFormat(Line->Children))
  978. return true;
  979. for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
  980. if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
  981. if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
  982. return true;
  983. if (Tok->is(TT_TemplateCloser) &&
  984. Tok->Previous->is(TT_TemplateCloser))
  985. return true;
  986. }
  987. }
  988. }
  989. return false;
  990. }
  991. int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  992. int AlignmentDiff = 0;
  993. for (const AnnotatedLine *Line : Lines) {
  994. AlignmentDiff += countVariableAlignments(Line->Children);
  995. for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
  996. if (!Tok->is(TT_PointerOrReference))
  997. continue;
  998. bool SpaceBefore =
  999. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  1000. bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
  1001. Tok->Next->WhitespaceRange.getEnd();
  1002. if (SpaceBefore && !SpaceAfter)
  1003. ++AlignmentDiff;
  1004. if (!SpaceBefore && SpaceAfter)
  1005. --AlignmentDiff;
  1006. }
  1007. }
  1008. return AlignmentDiff;
  1009. }
  1010. void
  1011. deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  1012. bool HasBinPackedFunction = false;
  1013. bool HasOnePerLineFunction = false;
  1014. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1015. if (!AnnotatedLines[i]->First->Next)
  1016. continue;
  1017. FormatToken *Tok = AnnotatedLines[i]->First->Next;
  1018. while (Tok->Next) {
  1019. if (Tok->PackingKind == PPK_BinPacked)
  1020. HasBinPackedFunction = true;
  1021. if (Tok->PackingKind == PPK_OnePerLine)
  1022. HasOnePerLineFunction = true;
  1023. Tok = Tok->Next;
  1024. }
  1025. }
  1026. if (Style.DerivePointerAlignment)
  1027. Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
  1028. ? FormatStyle::PAS_Left
  1029. : FormatStyle::PAS_Right;
  1030. if (Style.Standard == FormatStyle::LS_Auto)
  1031. Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
  1032. ? FormatStyle::LS_Cpp11
  1033. : FormatStyle::LS_Cpp03;
  1034. BinPackInconclusiveFunctions =
  1035. HasBinPackedFunction || !HasOnePerLineFunction;
  1036. }
  1037. bool BinPackInconclusiveFunctions;
  1038. FormattingAttemptStatus *Status;
  1039. };
  1040. // This class clean up the erroneous/redundant code around the given ranges in
  1041. // file.
  1042. class Cleaner : public TokenAnalyzer {
  1043. public:
  1044. Cleaner(const Environment &Env, const FormatStyle &Style)
  1045. : TokenAnalyzer(Env, Style),
  1046. DeletedTokens(FormatTokenLess(Env.getSourceManager())) {}
  1047. // FIXME: eliminate unused parameters.
  1048. std::pair<tooling::Replacements, unsigned>
  1049. analyze(TokenAnnotator &Annotator,
  1050. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  1051. FormatTokenLexer &Tokens) override {
  1052. // FIXME: in the current implementation the granularity of affected range
  1053. // is an annotated line. However, this is not sufficient. Furthermore,
  1054. // redundant code introduced by replacements does not necessarily
  1055. // intercept with ranges of replacements that result in the redundancy.
  1056. // To determine if some redundant code is actually introduced by
  1057. // replacements(e.g. deletions), we need to come up with a more
  1058. // sophisticated way of computing affected ranges.
  1059. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  1060. AnnotatedLines.end());
  1061. checkEmptyNamespace(AnnotatedLines);
  1062. for (auto &Line : AnnotatedLines) {
  1063. if (Line->Affected) {
  1064. cleanupRight(Line->First, tok::comma, tok::comma);
  1065. cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma);
  1066. cleanupRight(Line->First, tok::l_paren, tok::comma);
  1067. cleanupLeft(Line->First, tok::comma, tok::r_paren);
  1068. cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace);
  1069. cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace);
  1070. cleanupLeft(Line->First, TT_CtorInitializerColon, tok::equal);
  1071. }
  1072. }
  1073. return {generateFixes(), 0};
  1074. }
  1075. private:
  1076. bool containsOnlyComments(const AnnotatedLine &Line) {
  1077. for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) {
  1078. if (Tok->isNot(tok::comment))
  1079. return false;
  1080. }
  1081. return true;
  1082. }
  1083. // Iterate through all lines and remove any empty (nested) namespaces.
  1084. void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  1085. std::set<unsigned> DeletedLines;
  1086. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1087. auto &Line = *AnnotatedLines[i];
  1088. if (Line.startsWith(tok::kw_namespace) ||
  1089. Line.startsWith(tok::kw_inline, tok::kw_namespace)) {
  1090. checkEmptyNamespace(AnnotatedLines, i, i, DeletedLines);
  1091. }
  1092. }
  1093. for (auto Line : DeletedLines) {
  1094. FormatToken *Tok = AnnotatedLines[Line]->First;
  1095. while (Tok) {
  1096. deleteToken(Tok);
  1097. Tok = Tok->Next;
  1098. }
  1099. }
  1100. }
  1101. // The function checks if the namespace, which starts from \p CurrentLine, and
  1102. // its nested namespaces are empty and delete them if they are empty. It also
  1103. // sets \p NewLine to the last line checked.
  1104. // Returns true if the current namespace is empty.
  1105. bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  1106. unsigned CurrentLine, unsigned &NewLine,
  1107. std::set<unsigned> &DeletedLines) {
  1108. unsigned InitLine = CurrentLine, End = AnnotatedLines.size();
  1109. if (Style.BraceWrapping.AfterNamespace) {
  1110. // If the left brace is in a new line, we should consume it first so that
  1111. // it does not make the namespace non-empty.
  1112. // FIXME: error handling if there is no left brace.
  1113. if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) {
  1114. NewLine = CurrentLine;
  1115. return false;
  1116. }
  1117. } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) {
  1118. return false;
  1119. }
  1120. while (++CurrentLine < End) {
  1121. if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace))
  1122. break;
  1123. if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) ||
  1124. AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline,
  1125. tok::kw_namespace)) {
  1126. if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine,
  1127. DeletedLines))
  1128. return false;
  1129. CurrentLine = NewLine;
  1130. continue;
  1131. }
  1132. if (containsOnlyComments(*AnnotatedLines[CurrentLine]))
  1133. continue;
  1134. // If there is anything other than comments or nested namespaces in the
  1135. // current namespace, the namespace cannot be empty.
  1136. NewLine = CurrentLine;
  1137. return false;
  1138. }
  1139. NewLine = CurrentLine;
  1140. if (CurrentLine >= End)
  1141. return false;
  1142. // Check if the empty namespace is actually affected by changed ranges.
  1143. if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange(
  1144. AnnotatedLines[InitLine]->First->Tok.getLocation(),
  1145. AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc())))
  1146. return false;
  1147. for (unsigned i = InitLine; i <= CurrentLine; ++i) {
  1148. DeletedLines.insert(i);
  1149. }
  1150. return true;
  1151. }
  1152. // Checks pairs {start, start->next},..., {end->previous, end} and deletes one
  1153. // of the token in the pair if the left token has \p LK token kind and the
  1154. // right token has \p RK token kind. If \p DeleteLeft is true, the left token
  1155. // is deleted on match; otherwise, the right token is deleted.
  1156. template <typename LeftKind, typename RightKind>
  1157. void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK,
  1158. bool DeleteLeft) {
  1159. auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * {
  1160. for (auto *Res = Tok.Next; Res; Res = Res->Next)
  1161. if (!Res->is(tok::comment) &&
  1162. DeletedTokens.find(Res) == DeletedTokens.end())
  1163. return Res;
  1164. return nullptr;
  1165. };
  1166. for (auto *Left = Start; Left;) {
  1167. auto *Right = NextNotDeleted(*Left);
  1168. if (!Right)
  1169. break;
  1170. if (Left->is(LK) && Right->is(RK)) {
  1171. deleteToken(DeleteLeft ? Left : Right);
  1172. for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next)
  1173. deleteToken(Tok);
  1174. // If the right token is deleted, we should keep the left token
  1175. // unchanged and pair it with the new right token.
  1176. if (!DeleteLeft)
  1177. continue;
  1178. }
  1179. Left = Right;
  1180. }
  1181. }
  1182. template <typename LeftKind, typename RightKind>
  1183. void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) {
  1184. cleanupPair(Start, LK, RK, /*DeleteLeft=*/true);
  1185. }
  1186. template <typename LeftKind, typename RightKind>
  1187. void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) {
  1188. cleanupPair(Start, LK, RK, /*DeleteLeft=*/false);
  1189. }
  1190. // Delete the given token.
  1191. inline void deleteToken(FormatToken *Tok) {
  1192. if (Tok)
  1193. DeletedTokens.insert(Tok);
  1194. }
  1195. tooling::Replacements generateFixes() {
  1196. tooling::Replacements Fixes;
  1197. std::vector<FormatToken *> Tokens;
  1198. std::copy(DeletedTokens.begin(), DeletedTokens.end(),
  1199. std::back_inserter(Tokens));
  1200. // Merge multiple continuous token deletions into one big deletion so that
  1201. // the number of replacements can be reduced. This makes computing affected
  1202. // ranges more efficient when we run reformat on the changed code.
  1203. unsigned Idx = 0;
  1204. while (Idx < Tokens.size()) {
  1205. unsigned St = Idx, End = Idx;
  1206. while ((End + 1) < Tokens.size() &&
  1207. Tokens[End]->Next == Tokens[End + 1]) {
  1208. End++;
  1209. }
  1210. auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(),
  1211. Tokens[End]->Tok.getEndLoc());
  1212. auto Err =
  1213. Fixes.add(tooling::Replacement(Env.getSourceManager(), SR, ""));
  1214. // FIXME: better error handling. for now just print error message and skip
  1215. // for the release version.
  1216. if (Err) {
  1217. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  1218. assert(false && "Fixes must not conflict!");
  1219. }
  1220. Idx = End + 1;
  1221. }
  1222. return Fixes;
  1223. }
  1224. // Class for less-than inequality comparason for the set `RedundantTokens`.
  1225. // We store tokens in the order they appear in the translation unit so that
  1226. // we do not need to sort them in `generateFixes()`.
  1227. struct FormatTokenLess {
  1228. FormatTokenLess(const SourceManager &SM) : SM(SM) {}
  1229. bool operator()(const FormatToken *LHS, const FormatToken *RHS) const {
  1230. return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(),
  1231. RHS->Tok.getLocation());
  1232. }
  1233. const SourceManager &SM;
  1234. };
  1235. // Tokens to be deleted.
  1236. std::set<FormatToken *, FormatTokenLess> DeletedTokens;
  1237. };
  1238. struct IncludeDirective {
  1239. StringRef Filename;
  1240. StringRef Text;
  1241. unsigned Offset;
  1242. int Category;
  1243. };
  1244. } // end anonymous namespace
  1245. // Determines whether 'Ranges' intersects with ('Start', 'End').
  1246. static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
  1247. unsigned End) {
  1248. for (auto Range : Ranges) {
  1249. if (Range.getOffset() < End &&
  1250. Range.getOffset() + Range.getLength() > Start)
  1251. return true;
  1252. }
  1253. return false;
  1254. }
  1255. // Returns a pair (Index, OffsetToEOL) describing the position of the cursor
  1256. // before sorting/deduplicating. Index is the index of the include under the
  1257. // cursor in the original set of includes. If this include has duplicates, it is
  1258. // the index of the first of the duplicates as the others are going to be
  1259. // removed. OffsetToEOL describes the cursor's position relative to the end of
  1260. // its current line.
  1261. // If `Cursor` is not on any #include, `Index` will be UINT_MAX.
  1262. static std::pair<unsigned, unsigned>
  1263. FindCursorIndex(const SmallVectorImpl<IncludeDirective> &Includes,
  1264. const SmallVectorImpl<unsigned> &Indices, unsigned Cursor) {
  1265. unsigned CursorIndex = UINT_MAX;
  1266. unsigned OffsetToEOL = 0;
  1267. for (int i = 0, e = Includes.size(); i != e; ++i) {
  1268. unsigned Start = Includes[Indices[i]].Offset;
  1269. unsigned End = Start + Includes[Indices[i]].Text.size();
  1270. if (!(Cursor >= Start && Cursor < End))
  1271. continue;
  1272. CursorIndex = Indices[i];
  1273. OffsetToEOL = End - Cursor;
  1274. // Put the cursor on the only remaining #include among the duplicate
  1275. // #includes.
  1276. while (--i >= 0 && Includes[CursorIndex].Text == Includes[Indices[i]].Text)
  1277. CursorIndex = i;
  1278. break;
  1279. }
  1280. return std::make_pair(CursorIndex, OffsetToEOL);
  1281. }
  1282. // Sorts and deduplicate a block of includes given by 'Includes' alphabetically
  1283. // adding the necessary replacement to 'Replaces'. 'Includes' must be in strict
  1284. // source order.
  1285. // #include directives with the same text will be deduplicated, and only the
  1286. // first #include in the duplicate #includes remains. If the `Cursor` is
  1287. // provided and put on a deleted #include, it will be moved to the remaining
  1288. // #include in the duplicate #includes.
  1289. static void sortCppIncludes(const FormatStyle &Style,
  1290. const SmallVectorImpl<IncludeDirective> &Includes,
  1291. ArrayRef<tooling::Range> Ranges, StringRef FileName,
  1292. tooling::Replacements &Replaces, unsigned *Cursor) {
  1293. unsigned IncludesBeginOffset = Includes.front().Offset;
  1294. unsigned IncludesEndOffset =
  1295. Includes.back().Offset + Includes.back().Text.size();
  1296. unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset;
  1297. if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset))
  1298. return;
  1299. SmallVector<unsigned, 16> Indices;
  1300. for (unsigned i = 0, e = Includes.size(); i != e; ++i)
  1301. Indices.push_back(i);
  1302. std::stable_sort(
  1303. Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
  1304. return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
  1305. std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
  1306. });
  1307. // The index of the include on which the cursor will be put after
  1308. // sorting/deduplicating.
  1309. unsigned CursorIndex;
  1310. // The offset from cursor to the end of line.
  1311. unsigned CursorToEOLOffset;
  1312. if (Cursor)
  1313. std::tie(CursorIndex, CursorToEOLOffset) =
  1314. FindCursorIndex(Includes, Indices, *Cursor);
  1315. // Deduplicate #includes.
  1316. Indices.erase(std::unique(Indices.begin(), Indices.end(),
  1317. [&](unsigned LHSI, unsigned RHSI) {
  1318. return Includes[LHSI].Text == Includes[RHSI].Text;
  1319. }),
  1320. Indices.end());
  1321. int CurrentCategory = Includes.front().Category;
  1322. // If the #includes are out of order, we generate a single replacement fixing
  1323. // the entire block. Otherwise, no replacement is generated.
  1324. if (Indices.size() == Includes.size() &&
  1325. std::is_sorted(Indices.begin(), Indices.end()) &&
  1326. Style.IncludeBlocks == FormatStyle::IBS_Preserve)
  1327. return;
  1328. std::string result;
  1329. for (unsigned Index : Indices) {
  1330. if (!result.empty()) {
  1331. result += "\n";
  1332. if (Style.IncludeBlocks == FormatStyle::IBS_Regroup &&
  1333. CurrentCategory != Includes[Index].Category)
  1334. result += "\n";
  1335. }
  1336. result += Includes[Index].Text;
  1337. if (Cursor && CursorIndex == Index)
  1338. *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
  1339. CurrentCategory = Includes[Index].Category;
  1340. }
  1341. auto Err = Replaces.add(tooling::Replacement(
  1342. FileName, Includes.front().Offset, IncludesBlockSize, result));
  1343. // FIXME: better error handling. For now, just skip the replacement for the
  1344. // release version.
  1345. if (Err) {
  1346. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  1347. assert(false);
  1348. }
  1349. }
  1350. namespace {
  1351. // This class manages priorities of #include categories and calculates
  1352. // priorities for headers.
  1353. class IncludeCategoryManager {
  1354. public:
  1355. IncludeCategoryManager(const FormatStyle &Style, StringRef FileName)
  1356. : Style(Style), FileName(FileName) {
  1357. FileStem = llvm::sys::path::stem(FileName);
  1358. for (const auto &Category : Style.IncludeCategories)
  1359. CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
  1360. IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
  1361. FileName.endswith(".cpp") || FileName.endswith(".c++") ||
  1362. FileName.endswith(".cxx") || FileName.endswith(".m") ||
  1363. FileName.endswith(".mm");
  1364. }
  1365. // Returns the priority of the category which \p IncludeName belongs to.
  1366. // If \p CheckMainHeader is true and \p IncludeName is a main header, returns
  1367. // 0. Otherwise, returns the priority of the matching category or INT_MAX.
  1368. int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) {
  1369. int Ret = INT_MAX;
  1370. for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
  1371. if (CategoryRegexs[i].match(IncludeName)) {
  1372. Ret = Style.IncludeCategories[i].Priority;
  1373. break;
  1374. }
  1375. if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
  1376. Ret = 0;
  1377. return Ret;
  1378. }
  1379. private:
  1380. bool isMainHeader(StringRef IncludeName) const {
  1381. if (!IncludeName.startswith("\""))
  1382. return false;
  1383. StringRef HeaderStem =
  1384. llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
  1385. if (FileStem.startswith(HeaderStem) ||
  1386. FileStem.startswith_lower(HeaderStem)) {
  1387. llvm::Regex MainIncludeRegex(
  1388. (HeaderStem + Style.IncludeIsMainRegex).str(),
  1389. llvm::Regex::IgnoreCase);
  1390. if (MainIncludeRegex.match(FileStem))
  1391. return true;
  1392. }
  1393. return false;
  1394. }
  1395. const FormatStyle &Style;
  1396. bool IsMainFile;
  1397. StringRef FileName;
  1398. StringRef FileStem;
  1399. SmallVector<llvm::Regex, 4> CategoryRegexs;
  1400. };
  1401. const char IncludeRegexPattern[] =
  1402. R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
  1403. } // anonymous namespace
  1404. tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
  1405. ArrayRef<tooling::Range> Ranges,
  1406. StringRef FileName,
  1407. tooling::Replacements &Replaces,
  1408. unsigned *Cursor) {
  1409. unsigned Prev = 0;
  1410. unsigned SearchFrom = 0;
  1411. llvm::Regex IncludeRegex(IncludeRegexPattern);
  1412. SmallVector<StringRef, 4> Matches;
  1413. SmallVector<IncludeDirective, 16> IncludesInBlock;
  1414. // In compiled files, consider the first #include to be the main #include of
  1415. // the file if it is not a system #include. This ensures that the header
  1416. // doesn't have hidden dependencies
  1417. // (http://llvm.org/docs/CodingStandards.html#include-style).
  1418. //
  1419. // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
  1420. // cases where the first #include is unlikely to be the main header.
  1421. IncludeCategoryManager Categories(Style, FileName);
  1422. bool FirstIncludeBlock = true;
  1423. bool MainIncludeFound = false;
  1424. bool FormattingOff = false;
  1425. for (;;) {
  1426. auto Pos = Code.find('\n', SearchFrom);
  1427. StringRef Line =
  1428. Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
  1429. StringRef Trimmed = Line.trim();
  1430. if (Trimmed == "// clang-format off")
  1431. FormattingOff = true;
  1432. else if (Trimmed == "// clang-format on")
  1433. FormattingOff = false;
  1434. const bool EmptyLineSkipped =
  1435. Trimmed.empty() && (Style.IncludeBlocks == FormatStyle::IBS_Merge ||
  1436. Style.IncludeBlocks == FormatStyle::IBS_Regroup);
  1437. if (!FormattingOff && !Line.endswith("\\")) {
  1438. if (IncludeRegex.match(Line, &Matches)) {
  1439. StringRef IncludeName = Matches[2];
  1440. int Category = Categories.getIncludePriority(
  1441. IncludeName,
  1442. /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);
  1443. if (Category == 0)
  1444. MainIncludeFound = true;
  1445. IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
  1446. } else if (!IncludesInBlock.empty() && !EmptyLineSkipped) {
  1447. sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
  1448. Cursor);
  1449. IncludesInBlock.clear();
  1450. FirstIncludeBlock = false;
  1451. }
  1452. Prev = Pos + 1;
  1453. }
  1454. if (Pos == StringRef::npos || Pos + 1 == Code.size())
  1455. break;
  1456. SearchFrom = Pos + 1;
  1457. }
  1458. if (!IncludesInBlock.empty())
  1459. sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor);
  1460. return Replaces;
  1461. }
  1462. bool isMpegTS(StringRef Code) {
  1463. // MPEG transport streams use the ".ts" file extension. clang-format should
  1464. // not attempt to format those. MPEG TS' frame format starts with 0x47 every
  1465. // 189 bytes - detect that and return.
  1466. return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47;
  1467. }
  1468. bool isLikelyXml(StringRef Code) { return Code.ltrim().startswith("<"); }
  1469. tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
  1470. ArrayRef<tooling::Range> Ranges,
  1471. StringRef FileName, unsigned *Cursor) {
  1472. tooling::Replacements Replaces;
  1473. if (!Style.SortIncludes)
  1474. return Replaces;
  1475. if (isLikelyXml(Code))
  1476. return Replaces;
  1477. if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
  1478. isMpegTS(Code))
  1479. return Replaces;
  1480. if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
  1481. return sortJavaScriptImports(Style, Code, Ranges, FileName);
  1482. sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
  1483. return Replaces;
  1484. }
  1485. template <typename T>
  1486. static llvm::Expected<tooling::Replacements>
  1487. processReplacements(T ProcessFunc, StringRef Code,
  1488. const tooling::Replacements &Replaces,
  1489. const FormatStyle &Style) {
  1490. if (Replaces.empty())
  1491. return tooling::Replacements();
  1492. auto NewCode = applyAllReplacements(Code, Replaces);
  1493. if (!NewCode)
  1494. return NewCode.takeError();
  1495. std::vector<tooling::Range> ChangedRanges = Replaces.getAffectedRanges();
  1496. StringRef FileName = Replaces.begin()->getFilePath();
  1497. tooling::Replacements FormatReplaces =
  1498. ProcessFunc(Style, *NewCode, ChangedRanges, FileName);
  1499. return Replaces.merge(FormatReplaces);
  1500. }
  1501. llvm::Expected<tooling::Replacements>
  1502. formatReplacements(StringRef Code, const tooling::Replacements &Replaces,
  1503. const FormatStyle &Style) {
  1504. // We need to use lambda function here since there are two versions of
  1505. // `sortIncludes`.
  1506. auto SortIncludes = [](const FormatStyle &Style, StringRef Code,
  1507. std::vector<tooling::Range> Ranges,
  1508. StringRef FileName) -> tooling::Replacements {
  1509. return sortIncludes(Style, Code, Ranges, FileName);
  1510. };
  1511. auto SortedReplaces =
  1512. processReplacements(SortIncludes, Code, Replaces, Style);
  1513. if (!SortedReplaces)
  1514. return SortedReplaces.takeError();
  1515. // We need to use lambda function here since there are two versions of
  1516. // `reformat`.
  1517. auto Reformat = [](const FormatStyle &Style, StringRef Code,
  1518. std::vector<tooling::Range> Ranges,
  1519. StringRef FileName) -> tooling::Replacements {
  1520. return reformat(Style, Code, Ranges, FileName);
  1521. };
  1522. return processReplacements(Reformat, Code, *SortedReplaces, Style);
  1523. }
  1524. namespace {
  1525. inline bool isHeaderInsertion(const tooling::Replacement &Replace) {
  1526. return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 &&
  1527. llvm::Regex(IncludeRegexPattern).match(Replace.getReplacementText());
  1528. }
  1529. inline bool isHeaderDeletion(const tooling::Replacement &Replace) {
  1530. return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1;
  1531. }
  1532. // Returns the offset after skipping a sequence of tokens, matched by \p
  1533. // GetOffsetAfterSequence, from the start of the code.
  1534. // \p GetOffsetAfterSequence should be a function that matches a sequence of
  1535. // tokens and returns an offset after the sequence.
  1536. unsigned getOffsetAfterTokenSequence(
  1537. StringRef FileName, StringRef Code, const FormatStyle &Style,
  1538. llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)>
  1539. GetOffsetAfterSequence) {
  1540. std::unique_ptr<Environment> Env =
  1541. Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{});
  1542. const SourceManager &SourceMgr = Env->getSourceManager();
  1543. Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr,
  1544. getFormattingLangOpts(Style));
  1545. Token Tok;
  1546. // Get the first token.
  1547. Lex.LexFromRawLexer(Tok);
  1548. return GetOffsetAfterSequence(SourceMgr, Lex, Tok);
  1549. }
  1550. // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,
  1551. // \p Tok will be the token after this directive; otherwise, it can be any token
  1552. // after the given \p Tok (including \p Tok).
  1553. bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {
  1554. bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
  1555. Tok.is(tok::raw_identifier) &&
  1556. Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
  1557. Tok.is(tok::raw_identifier);
  1558. if (Matched)
  1559. Lex.LexFromRawLexer(Tok);
  1560. return Matched;
  1561. }
  1562. void skipComments(Lexer &Lex, Token &Tok) {
  1563. while (Tok.is(tok::comment))
  1564. if (Lex.LexFromRawLexer(Tok))
  1565. return;
  1566. }
  1567. // Returns the offset after header guard directives and any comments
  1568. // before/after header guards. If no header guard presents in the code, this
  1569. // will returns the offset after skipping all comments from the start of the
  1570. // code.
  1571. unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
  1572. StringRef Code,
  1573. const FormatStyle &Style) {
  1574. return getOffsetAfterTokenSequence(
  1575. FileName, Code, Style,
  1576. [](const SourceManager &SM, Lexer &Lex, Token Tok) {
  1577. skipComments(Lex, Tok);
  1578. unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());
  1579. if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
  1580. skipComments(Lex, Tok);
  1581. if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))
  1582. return SM.getFileOffset(Tok.getLocation());
  1583. }
  1584. return InitialOffset;
  1585. });
  1586. }
  1587. // Check if a sequence of tokens is like
  1588. // "#include ("header.h" | <header.h>)".
  1589. // If it is, \p Tok will be the token after this directive; otherwise, it can be
  1590. // any token after the given \p Tok (including \p Tok).
  1591. bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
  1592. auto Matched = [&]() {
  1593. Lex.LexFromRawLexer(Tok);
  1594. return true;
  1595. };
  1596. if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
  1597. Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") {
  1598. if (Lex.LexFromRawLexer(Tok))
  1599. return false;
  1600. if (Tok.is(tok::string_literal))
  1601. return Matched();
  1602. if (Tok.is(tok::less)) {
  1603. while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {
  1604. }
  1605. if (Tok.is(tok::greater))
  1606. return Matched();
  1607. }
  1608. }
  1609. return false;
  1610. }
  1611. // Returns the offset of the last #include directive after which a new
  1612. // #include can be inserted. This ignores #include's after the #include block(s)
  1613. // in the beginning of a file to avoid inserting headers into code sections
  1614. // where new #include's should not be added by default.
  1615. // These code sections include:
  1616. // - raw string literals (containing #include).
  1617. // - #if blocks.
  1618. // - Special #include's among declarations (e.g. functions).
  1619. //
  1620. // If no #include after which a new #include can be inserted, this returns the
  1621. // offset after skipping all comments from the start of the code.
  1622. // Inserting after an #include is not allowed if it comes after code that is not
  1623. // #include (e.g. pre-processing directive that is not #include, declarations).
  1624. unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
  1625. const FormatStyle &Style) {
  1626. return getOffsetAfterTokenSequence(
  1627. FileName, Code, Style,
  1628. [](const SourceManager &SM, Lexer &Lex, Token Tok) {
  1629. skipComments(Lex, Tok);
  1630. unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());
  1631. while (checkAndConsumeInclusiveDirective(Lex, Tok))
  1632. MaxOffset = SM.getFileOffset(Tok.getLocation());
  1633. return MaxOffset;
  1634. });
  1635. }
  1636. bool isDeletedHeader(llvm::StringRef HeaderName,
  1637. const std::set<llvm::StringRef> &HeadersToDelete) {
  1638. return HeadersToDelete.count(HeaderName) ||
  1639. HeadersToDelete.count(HeaderName.trim("\"<>"));
  1640. }
  1641. // FIXME: insert empty lines between newly created blocks.
  1642. tooling::Replacements
  1643. fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
  1644. const FormatStyle &Style) {
  1645. if (!Style.isCpp())
  1646. return Replaces;
  1647. tooling::Replacements HeaderInsertions;
  1648. std::set<llvm::StringRef> HeadersToDelete;
  1649. tooling::Replacements Result;
  1650. for (const auto &R : Replaces) {
  1651. if (isHeaderInsertion(R)) {
  1652. // Replacements from \p Replaces must be conflict-free already, so we can
  1653. // simply consume the error.
  1654. llvm::consumeError(HeaderInsertions.add(R));
  1655. } else if (isHeaderDeletion(R)) {
  1656. HeadersToDelete.insert(R.getReplacementText());
  1657. } else if (R.getOffset() == UINT_MAX) {
  1658. llvm::errs() << "Insertions other than header #include insertion are "
  1659. "not supported! "
  1660. << R.getReplacementText() << "\n";
  1661. } else {
  1662. llvm::consumeError(Result.add(R));
  1663. }
  1664. }
  1665. if (HeaderInsertions.empty() && HeadersToDelete.empty())
  1666. return Replaces;
  1667. llvm::Regex IncludeRegex(IncludeRegexPattern);
  1668. llvm::Regex DefineRegex(R"(^[\t\ ]*#[\t\ ]*define[\t\ ]*[^\\]*$)");
  1669. SmallVector<StringRef, 4> Matches;
  1670. StringRef FileName = Replaces.begin()->getFilePath();
  1671. IncludeCategoryManager Categories(Style, FileName);
  1672. // Record the offset of the end of the last include in each category.
  1673. std::map<int, int> CategoryEndOffsets;
  1674. // All possible priorities.
  1675. // Add 0 for main header and INT_MAX for headers that are not in any category.
  1676. std::set<int> Priorities = {0, INT_MAX};
  1677. for (const auto &Category : Style.IncludeCategories)
  1678. Priorities.insert(Category.Priority);
  1679. int FirstIncludeOffset = -1;
  1680. // All new headers should be inserted after this offset.
  1681. unsigned MinInsertOffset =
  1682. getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style);
  1683. StringRef TrimmedCode = Code.drop_front(MinInsertOffset);
  1684. // Max insertion offset in the original code.
  1685. unsigned MaxInsertOffset =
  1686. MinInsertOffset +
  1687. getMaxHeaderInsertionOffset(FileName, TrimmedCode, Style);
  1688. SmallVector<StringRef, 32> Lines;
  1689. TrimmedCode.split(Lines, '\n');
  1690. unsigned Offset = MinInsertOffset;
  1691. unsigned NextLineOffset;
  1692. std::set<StringRef> ExistingIncludes;
  1693. for (auto Line : Lines) {
  1694. NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
  1695. if (IncludeRegex.match(Line, &Matches)) {
  1696. // The header name with quotes or angle brackets.
  1697. StringRef IncludeName = Matches[2];
  1698. ExistingIncludes.insert(IncludeName);
  1699. // Only record the offset of current #include if we can insert after it.
  1700. if (Offset <= MaxInsertOffset) {
  1701. int Category = Categories.getIncludePriority(
  1702. IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
  1703. CategoryEndOffsets[Category] = NextLineOffset;
  1704. if (FirstIncludeOffset < 0)
  1705. FirstIncludeOffset = Offset;
  1706. }
  1707. if (isDeletedHeader(IncludeName, HeadersToDelete)) {
  1708. // If this is the last line without trailing newline, we need to make
  1709. // sure we don't delete across the file boundary.
  1710. unsigned Length = std::min(Line.size() + 1, Code.size() - Offset);
  1711. llvm::Error Err =
  1712. Result.add(tooling::Replacement(FileName, Offset, Length, ""));
  1713. if (Err) {
  1714. // Ignore the deletion on conflict.
  1715. llvm::errs() << "Failed to add header deletion replacement for "
  1716. << IncludeName << ": " << llvm::toString(std::move(Err))
  1717. << "\n";
  1718. }
  1719. }
  1720. }
  1721. Offset = NextLineOffset;
  1722. }
  1723. // Populate CategoryEndOfssets:
  1724. // - Ensure that CategoryEndOffset[Highest] is always populated.
  1725. // - If CategoryEndOffset[Priority] isn't set, use the next higher value that
  1726. // is set, up to CategoryEndOffset[Highest].
  1727. auto Highest = Priorities.begin();
  1728. if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
  1729. if (FirstIncludeOffset >= 0)
  1730. CategoryEndOffsets[*Highest] = FirstIncludeOffset;
  1731. else
  1732. CategoryEndOffsets[*Highest] = MinInsertOffset;
  1733. }
  1734. // By this point, CategoryEndOffset[Highest] is always set appropriately:
  1735. // - to an appropriate location before/after existing #includes, or
  1736. // - to right after the header guard, or
  1737. // - to the beginning of the file.
  1738. for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
  1739. if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
  1740. CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
  1741. bool NeedNewLineAtEnd = !Code.empty() && Code.back() != '\n';
  1742. for (const auto &R : HeaderInsertions) {
  1743. auto IncludeDirective = R.getReplacementText();
  1744. bool Matched = IncludeRegex.match(IncludeDirective, &Matches);
  1745. assert(Matched && "Header insertion replacement must have replacement text "
  1746. "'#include ...'");
  1747. (void)Matched;
  1748. auto IncludeName = Matches[2];
  1749. if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) {
  1750. DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName
  1751. << "\n");
  1752. continue;
  1753. }
  1754. int Category =
  1755. Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
  1756. Offset = CategoryEndOffsets[Category];
  1757. std::string NewInclude = !IncludeDirective.endswith("\n")
  1758. ? (IncludeDirective + "\n").str()
  1759. : IncludeDirective.str();
  1760. // When inserting headers at end of the code, also append '\n' to the code
  1761. // if it does not end with '\n'.
  1762. if (NeedNewLineAtEnd && Offset == Code.size()) {
  1763. NewInclude = "\n" + NewInclude;
  1764. NeedNewLineAtEnd = false;
  1765. }
  1766. auto NewReplace = tooling::Replacement(FileName, Offset, 0, NewInclude);
  1767. auto Err = Result.add(NewReplace);
  1768. if (Err) {
  1769. llvm::consumeError(std::move(Err));
  1770. unsigned NewOffset = Result.getShiftedCodePosition(Offset);
  1771. NewReplace = tooling::Replacement(FileName, NewOffset, 0, NewInclude);
  1772. Result = Result.merge(tooling::Replacements(NewReplace));
  1773. }
  1774. }
  1775. return Result;
  1776. }
  1777. } // anonymous namespace
  1778. llvm::Expected<tooling::Replacements>
  1779. cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces,
  1780. const FormatStyle &Style) {
  1781. // We need to use lambda function here since there are two versions of
  1782. // `cleanup`.
  1783. auto Cleanup = [](const FormatStyle &Style, StringRef Code,
  1784. std::vector<tooling::Range> Ranges,
  1785. StringRef FileName) -> tooling::Replacements {
  1786. return cleanup(Style, Code, Ranges, FileName);
  1787. };
  1788. // Make header insertion replacements insert new headers into correct blocks.
  1789. tooling::Replacements NewReplaces =
  1790. fixCppIncludeInsertions(Code, Replaces, Style);
  1791. return processReplacements(Cleanup, Code, NewReplaces, Style);
  1792. }
  1793. namespace internal {
  1794. std::pair<tooling::Replacements, unsigned>
  1795. reformat(const FormatStyle &Style, StringRef Code,
  1796. ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
  1797. unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName,
  1798. FormattingAttemptStatus *Status) {
  1799. FormatStyle Expanded = expandPresets(Style);
  1800. if (Expanded.DisableFormat)
  1801. return {tooling::Replacements(), 0};
  1802. if (isLikelyXml(Code))
  1803. return {tooling::Replacements(), 0};
  1804. if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
  1805. return {tooling::Replacements(), 0};
  1806. typedef std::function<std::pair<tooling::Replacements, unsigned>(
  1807. const Environment &)>
  1808. AnalyzerPass;
  1809. SmallVector<AnalyzerPass, 4> Passes;
  1810. if (Style.Language == FormatStyle::LK_Cpp) {
  1811. if (Style.FixNamespaceComments)
  1812. Passes.emplace_back([&](const Environment &Env) {
  1813. return NamespaceEndCommentsFixer(Env, Expanded).process();
  1814. });
  1815. if (Style.SortUsingDeclarations)
  1816. Passes.emplace_back([&](const Environment &Env) {
  1817. return UsingDeclarationsSorter(Env, Expanded).process();
  1818. });
  1819. }
  1820. if (Style.Language == FormatStyle::LK_JavaScript &&
  1821. Style.JavaScriptQuotes != FormatStyle::JSQS_Leave)
  1822. Passes.emplace_back([&](const Environment &Env) {
  1823. return JavaScriptRequoter(Env, Expanded).process();
  1824. });
  1825. Passes.emplace_back([&](const Environment &Env) {
  1826. return Formatter(Env, Expanded, Status).process();
  1827. });
  1828. std::unique_ptr<Environment> Env = Environment::CreateVirtualEnvironment(
  1829. Code, FileName, Ranges, FirstStartColumn, NextStartColumn,
  1830. LastStartColumn);
  1831. llvm::Optional<std::string> CurrentCode = None;
  1832. tooling::Replacements Fixes;
  1833. unsigned Penalty = 0;
  1834. for (size_t I = 0, E = Passes.size(); I < E; ++I) {
  1835. std::pair<tooling::Replacements, unsigned> PassFixes = Passes[I](*Env);
  1836. auto NewCode = applyAllReplacements(
  1837. CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes.first);
  1838. if (NewCode) {
  1839. Fixes = Fixes.merge(PassFixes.first);
  1840. Penalty += PassFixes.second;
  1841. if (I + 1 < E) {
  1842. CurrentCode = std::move(*NewCode);
  1843. Env = Environment::CreateVirtualEnvironment(
  1844. *CurrentCode, FileName,
  1845. tooling::calculateRangesAfterReplacements(Fixes, Ranges),
  1846. FirstStartColumn, NextStartColumn, LastStartColumn);
  1847. }
  1848. }
  1849. }
  1850. return {Fixes, Penalty};
  1851. }
  1852. } // namespace internal
  1853. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1854. ArrayRef<tooling::Range> Ranges,
  1855. StringRef FileName,
  1856. FormattingAttemptStatus *Status) {
  1857. return internal::reformat(Style, Code, Ranges,
  1858. /*FirstStartColumn=*/0,
  1859. /*NextStartColumn=*/0,
  1860. /*LastStartColumn=*/0, FileName, Status)
  1861. .first;
  1862. }
  1863. tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code,
  1864. ArrayRef<tooling::Range> Ranges,
  1865. StringRef FileName) {
  1866. // cleanups only apply to C++ (they mostly concern ctor commas etc.)
  1867. if (Style.Language != FormatStyle::LK_Cpp)
  1868. return tooling::Replacements();
  1869. std::unique_ptr<Environment> Env =
  1870. Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
  1871. Cleaner Clean(*Env, Style);
  1872. return Clean.process().first;
  1873. }
  1874. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1875. ArrayRef<tooling::Range> Ranges,
  1876. StringRef FileName, bool *IncompleteFormat) {
  1877. FormattingAttemptStatus Status;
  1878. auto Result = reformat(Style, Code, Ranges, FileName, &Status);
  1879. if (!Status.FormatComplete)
  1880. *IncompleteFormat = true;
  1881. return Result;
  1882. }
  1883. tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style,
  1884. StringRef Code,
  1885. ArrayRef<tooling::Range> Ranges,
  1886. StringRef FileName) {
  1887. std::unique_ptr<Environment> Env =
  1888. Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
  1889. NamespaceEndCommentsFixer Fix(*Env, Style);
  1890. return Fix.process().first;
  1891. }
  1892. tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
  1893. StringRef Code,
  1894. ArrayRef<tooling::Range> Ranges,
  1895. StringRef FileName) {
  1896. std::unique_ptr<Environment> Env =
  1897. Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
  1898. UsingDeclarationsSorter Sorter(*Env, Style);
  1899. return Sorter.process().first;
  1900. }
  1901. LangOptions getFormattingLangOpts(const FormatStyle &Style) {
  1902. LangOptions LangOpts;
  1903. LangOpts.CPlusPlus = 1;
  1904. LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1905. LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1906. LangOpts.CPlusPlus17 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1907. LangOpts.CPlusPlus2a = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1908. LangOpts.LineComment = 1;
  1909. bool AlternativeOperators = Style.isCpp();
  1910. LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
  1911. LangOpts.Bool = 1;
  1912. LangOpts.ObjC1 = 1;
  1913. LangOpts.ObjC2 = 1;
  1914. LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
  1915. LangOpts.DeclSpecKeyword = 1; // To get __declspec.
  1916. return LangOpts;
  1917. }
  1918. const char *StyleOptionHelpDescription =
  1919. "Coding style, currently supports:\n"
  1920. " LLVM, Google, Chromium, Mozilla, WebKit.\n"
  1921. "Use -style=file to load style configuration from\n"
  1922. ".clang-format file located in one of the parent\n"
  1923. "directories of the source file (or current\n"
  1924. "directory for stdin).\n"
  1925. "Use -style=\"{key: value, ...}\" to set specific\n"
  1926. "parameters, e.g.:\n"
  1927. " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
  1928. static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
  1929. if (FileName.endswith(".java"))
  1930. return FormatStyle::LK_Java;
  1931. if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts"))
  1932. return FormatStyle::LK_JavaScript; // JavaScript or TypeScript.
  1933. if (FileName.endswith(".m") || FileName.endswith(".mm"))
  1934. return FormatStyle::LK_ObjC;
  1935. if (FileName.endswith_lower(".proto") ||
  1936. FileName.endswith_lower(".protodevel"))
  1937. return FormatStyle::LK_Proto;
  1938. if (FileName.endswith_lower(".textpb") ||
  1939. FileName.endswith_lower(".pb.txt") ||
  1940. FileName.endswith_lower(".textproto") ||
  1941. FileName.endswith_lower(".asciipb"))
  1942. return FormatStyle::LK_TextProto;
  1943. if (FileName.endswith_lower(".td"))
  1944. return FormatStyle::LK_TableGen;
  1945. return FormatStyle::LK_Cpp;
  1946. }
  1947. llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
  1948. StringRef FallbackStyleName,
  1949. StringRef Code, vfs::FileSystem *FS) {
  1950. if (!FS) {
  1951. FS = vfs::getRealFileSystem().get();
  1952. }
  1953. FormatStyle Style = getLLVMStyle();
  1954. Style.Language = getLanguageByFileName(FileName);
  1955. // This is a very crude detection of whether a header contains ObjC code that
  1956. // should be improved over time and probably be done on tokens, not one the
  1957. // bare content of the file.
  1958. if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h") &&
  1959. (Code.contains("\n- (") || Code.contains("\n+ (") ||
  1960. Code.contains("\n@end\n") || Code.contains("\n@end ") ||
  1961. Code.endswith("@end")))
  1962. Style.Language = FormatStyle::LK_ObjC;
  1963. FormatStyle FallbackStyle = getNoStyle();
  1964. if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
  1965. return make_string_error("Invalid fallback style \"" + FallbackStyleName);
  1966. if (StyleName.startswith("{")) {
  1967. // Parse YAML/JSON style from the command line.
  1968. if (std::error_code ec = parseConfiguration(StyleName, &Style))
  1969. return make_string_error("Error parsing -style: " + ec.message());
  1970. return Style;
  1971. }
  1972. if (!StyleName.equals_lower("file")) {
  1973. if (!getPredefinedStyle(StyleName, Style.Language, &Style))
  1974. return make_string_error("Invalid value for -style");
  1975. return Style;
  1976. }
  1977. // Look for .clang-format/_clang-format file in the file's parent directories.
  1978. SmallString<128> UnsuitableConfigFiles;
  1979. SmallString<128> Path(FileName);
  1980. if (std::error_code EC = FS->makeAbsolute(Path))
  1981. return make_string_error(EC.message());
  1982. for (StringRef Directory = Path; !Directory.empty();
  1983. Directory = llvm::sys::path::parent_path(Directory)) {
  1984. auto Status = FS->status(Directory);
  1985. if (!Status ||
  1986. Status->getType() != llvm::sys::fs::file_type::directory_file) {
  1987. continue;
  1988. }
  1989. SmallString<128> ConfigFile(Directory);
  1990. llvm::sys::path::append(ConfigFile, ".clang-format");
  1991. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1992. Status = FS->status(ConfigFile.str());
  1993. bool FoundConfigFile =
  1994. Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
  1995. if (!FoundConfigFile) {
  1996. // Try _clang-format too, since dotfiles are not commonly used on Windows.
  1997. ConfigFile = Directory;
  1998. llvm::sys::path::append(ConfigFile, "_clang-format");
  1999. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  2000. Status = FS->status(ConfigFile.str());
  2001. FoundConfigFile = Status && (Status->getType() ==
  2002. llvm::sys::fs::file_type::regular_file);
  2003. }
  2004. if (FoundConfigFile) {
  2005. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
  2006. FS->getBufferForFile(ConfigFile.str());
  2007. if (std::error_code EC = Text.getError())
  2008. return make_string_error(EC.message());
  2009. if (std::error_code ec =
  2010. parseConfiguration(Text.get()->getBuffer(), &Style)) {
  2011. if (ec == ParseError::Unsuitable) {
  2012. if (!UnsuitableConfigFiles.empty())
  2013. UnsuitableConfigFiles.append(", ");
  2014. UnsuitableConfigFiles.append(ConfigFile);
  2015. continue;
  2016. }
  2017. return make_string_error("Error reading " + ConfigFile + ": " +
  2018. ec.message());
  2019. }
  2020. DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
  2021. return Style;
  2022. }
  2023. }
  2024. if (!UnsuitableConfigFiles.empty())
  2025. return make_string_error("Configuration file(s) do(es) not support " +
  2026. getLanguageName(Style.Language) + ": " +
  2027. UnsuitableConfigFiles);
  2028. return FallbackStyle;
  2029. }
  2030. } // namespace format
  2031. } // namespace clang