Format.cpp 88 KB

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