Format.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. //===--- Format.cpp - Format C++ code -------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief This file implements functions declared in Format.h. This will be
  12. /// split into separate files as we go.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "clang/Format/Format.h"
  16. #include "AffectedRangeManager.h"
  17. #include "ContinuationIndenter.h"
  18. #include "FormatTokenLexer.h"
  19. #include "SortJavaScriptImports.h"
  20. #include "TokenAnalyzer.h"
  21. #include "TokenAnnotator.h"
  22. #include "UnwrappedLineFormatter.h"
  23. #include "UnwrappedLineParser.h"
  24. #include "WhitespaceManager.h"
  25. #include "clang/Basic/Diagnostic.h"
  26. #include "clang/Basic/DiagnosticOptions.h"
  27. #include "clang/Basic/SourceManager.h"
  28. #include "clang/Basic/VirtualFileSystem.h"
  29. #include "clang/Lex/Lexer.h"
  30. #include "llvm/ADT/STLExtras.h"
  31. #include "llvm/Support/Allocator.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/Path.h"
  34. #include "llvm/Support/Regex.h"
  35. #include "llvm/Support/YAMLTraits.h"
  36. #include <algorithm>
  37. #include <memory>
  38. #include <queue>
  39. #include <string>
  40. #define DEBUG_TYPE "format-formatter"
  41. using clang::format::FormatStyle;
  42. LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
  43. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
  44. namespace llvm {
  45. namespace yaml {
  46. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
  47. static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
  48. IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
  49. IO.enumCase(Value, "Java", FormatStyle::LK_Java);
  50. IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
  51. IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
  52. IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen);
  53. }
  54. };
  55. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
  56. static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
  57. IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
  58. IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
  59. IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
  60. IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
  61. IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
  62. }
  63. };
  64. template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
  65. static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
  66. IO.enumCase(Value, "Never", FormatStyle::UT_Never);
  67. IO.enumCase(Value, "false", FormatStyle::UT_Never);
  68. IO.enumCase(Value, "Always", FormatStyle::UT_Always);
  69. IO.enumCase(Value, "true", FormatStyle::UT_Always);
  70. IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
  71. IO.enumCase(Value, "ForContinuationAndIndentation",
  72. FormatStyle::UT_ForContinuationAndIndentation);
  73. }
  74. };
  75. template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
  76. static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) {
  77. IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave);
  78. IO.enumCase(Value, "Single", FormatStyle::JSQS_Single);
  79. IO.enumCase(Value, "Double", FormatStyle::JSQS_Double);
  80. }
  81. };
  82. template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
  83. static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
  84. IO.enumCase(Value, "None", FormatStyle::SFS_None);
  85. IO.enumCase(Value, "false", FormatStyle::SFS_None);
  86. IO.enumCase(Value, "All", FormatStyle::SFS_All);
  87. IO.enumCase(Value, "true", FormatStyle::SFS_All);
  88. IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
  89. IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
  90. }
  91. };
  92. template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
  93. static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
  94. IO.enumCase(Value, "All", FormatStyle::BOS_All);
  95. IO.enumCase(Value, "true", FormatStyle::BOS_All);
  96. IO.enumCase(Value, "None", FormatStyle::BOS_None);
  97. IO.enumCase(Value, "false", FormatStyle::BOS_None);
  98. IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
  99. }
  100. };
  101. template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
  102. static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
  103. IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
  104. IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
  105. IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
  106. IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
  107. IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
  108. IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
  109. IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
  110. IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
  111. }
  112. };
  113. template <>
  114. struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> {
  115. static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) {
  116. IO.enumCase(Value, "None", FormatStyle::RTBS_None);
  117. IO.enumCase(Value, "All", FormatStyle::RTBS_All);
  118. IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel);
  119. IO.enumCase(Value, "TopLevelDefinitions",
  120. FormatStyle::RTBS_TopLevelDefinitions);
  121. IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions);
  122. }
  123. };
  124. template <>
  125. struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
  126. static void
  127. enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
  128. IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
  129. IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
  130. IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
  131. // For backward compatibility.
  132. IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
  133. IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
  134. }
  135. };
  136. template <>
  137. struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
  138. static void enumeration(IO &IO,
  139. FormatStyle::NamespaceIndentationKind &Value) {
  140. IO.enumCase(Value, "None", FormatStyle::NI_None);
  141. IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
  142. IO.enumCase(Value, "All", FormatStyle::NI_All);
  143. }
  144. };
  145. template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
  146. static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
  147. IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
  148. IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
  149. IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
  150. // For backward compatibility.
  151. IO.enumCase(Value, "true", FormatStyle::BAS_Align);
  152. IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
  153. }
  154. };
  155. template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
  156. static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
  157. IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
  158. IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
  159. IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
  160. // For backward compatibility.
  161. IO.enumCase(Value, "true", FormatStyle::PAS_Left);
  162. IO.enumCase(Value, "false", FormatStyle::PAS_Right);
  163. }
  164. };
  165. template <>
  166. struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
  167. static void enumeration(IO &IO,
  168. FormatStyle::SpaceBeforeParensOptions &Value) {
  169. IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
  170. IO.enumCase(Value, "ControlStatements",
  171. FormatStyle::SBPO_ControlStatements);
  172. IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
  173. // For backward compatibility.
  174. IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
  175. IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
  176. }
  177. };
  178. template <> struct MappingTraits<FormatStyle> {
  179. static void mapping(IO &IO, FormatStyle &Style) {
  180. // When reading, read the language first, we need it for getPredefinedStyle.
  181. IO.mapOptional("Language", Style.Language);
  182. if (IO.outputting()) {
  183. StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
  184. "Mozilla", "WebKit", "GNU"};
  185. ArrayRef<StringRef> Styles(StylesArray);
  186. for (size_t i = 0, e = Styles.size(); i < e; ++i) {
  187. StringRef StyleName(Styles[i]);
  188. FormatStyle PredefinedStyle;
  189. if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
  190. Style == PredefinedStyle) {
  191. IO.mapOptional("# BasedOnStyle", StyleName);
  192. break;
  193. }
  194. }
  195. } else {
  196. StringRef BasedOnStyle;
  197. IO.mapOptional("BasedOnStyle", BasedOnStyle);
  198. if (!BasedOnStyle.empty()) {
  199. FormatStyle::LanguageKind OldLanguage = Style.Language;
  200. FormatStyle::LanguageKind Language =
  201. ((FormatStyle *)IO.getContext())->Language;
  202. if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
  203. IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
  204. return;
  205. }
  206. Style.Language = OldLanguage;
  207. }
  208. }
  209. // For backward compatibility.
  210. if (!IO.outputting()) {
  211. IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
  212. IO.mapOptional("IndentFunctionDeclarationAfterType",
  213. Style.IndentWrappedFunctionNames);
  214. IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
  215. IO.mapOptional("SpaceAfterControlStatementKeyword",
  216. Style.SpaceBeforeParens);
  217. }
  218. IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
  219. IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
  220. IO.mapOptional("AlignConsecutiveAssignments",
  221. Style.AlignConsecutiveAssignments);
  222. IO.mapOptional("AlignConsecutiveDeclarations",
  223. Style.AlignConsecutiveDeclarations);
  224. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
  225. IO.mapOptional("AlignOperands", Style.AlignOperands);
  226. IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
  227. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  228. Style.AllowAllParametersOfDeclarationOnNextLine);
  229. IO.mapOptional("AllowShortBlocksOnASingleLine",
  230. Style.AllowShortBlocksOnASingleLine);
  231. IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
  232. Style.AllowShortCaseLabelsOnASingleLine);
  233. IO.mapOptional("AllowShortFunctionsOnASingleLine",
  234. Style.AllowShortFunctionsOnASingleLine);
  235. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  236. Style.AllowShortIfStatementsOnASingleLine);
  237. IO.mapOptional("AllowShortLoopsOnASingleLine",
  238. Style.AllowShortLoopsOnASingleLine);
  239. IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
  240. Style.AlwaysBreakAfterDefinitionReturnType);
  241. IO.mapOptional("AlwaysBreakAfterReturnType",
  242. Style.AlwaysBreakAfterReturnType);
  243. // If AlwaysBreakAfterDefinitionReturnType was specified but
  244. // AlwaysBreakAfterReturnType was not, initialize the latter from the
  245. // former for backwards compatibility.
  246. if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None &&
  247. Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) {
  248. if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All)
  249. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  250. else if (Style.AlwaysBreakAfterDefinitionReturnType ==
  251. FormatStyle::DRTBS_TopLevel)
  252. Style.AlwaysBreakAfterReturnType =
  253. FormatStyle::RTBS_TopLevelDefinitions;
  254. }
  255. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  256. Style.AlwaysBreakBeforeMultilineStrings);
  257. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  258. Style.AlwaysBreakTemplateDeclarations);
  259. IO.mapOptional("BinPackArguments", Style.BinPackArguments);
  260. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  261. IO.mapOptional("BraceWrapping", Style.BraceWrapping);
  262. IO.mapOptional("BreakBeforeBinaryOperators",
  263. Style.BreakBeforeBinaryOperators);
  264. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  265. IO.mapOptional("BreakBeforeTernaryOperators",
  266. Style.BreakBeforeTernaryOperators);
  267. IO.mapOptional("BreakConstructorInitializersBeforeComma",
  268. Style.BreakConstructorInitializersBeforeComma);
  269. IO.mapOptional("BreakAfterJavaFieldAnnotations",
  270. Style.BreakAfterJavaFieldAnnotations);
  271. IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
  272. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  273. IO.mapOptional("CommentPragmas", Style.CommentPragmas);
  274. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  275. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  276. IO.mapOptional("ConstructorInitializerIndentWidth",
  277. Style.ConstructorInitializerIndentWidth);
  278. IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
  279. IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
  280. IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
  281. IO.mapOptional("DisableFormat", Style.DisableFormat);
  282. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  283. Style.ExperimentalAutoDetectBinPacking);
  284. IO.mapOptional("ForEachMacros", Style.ForEachMacros);
  285. IO.mapOptional("IncludeCategories", Style.IncludeCategories);
  286. IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
  287. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  288. IO.mapOptional("IndentWidth", Style.IndentWidth);
  289. IO.mapOptional("IndentWrappedFunctionNames",
  290. Style.IndentWrappedFunctionNames);
  291. IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes);
  292. IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports);
  293. IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
  294. Style.KeepEmptyLinesAtTheStartOfBlocks);
  295. IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
  296. IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
  297. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  298. IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
  299. IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
  300. IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
  301. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  302. Style.ObjCSpaceBeforeProtocolList);
  303. IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
  304. Style.PenaltyBreakBeforeFirstCallParameter);
  305. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  306. IO.mapOptional("PenaltyBreakFirstLessLess",
  307. Style.PenaltyBreakFirstLessLess);
  308. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  309. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  310. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  311. Style.PenaltyReturnTypeOnItsOwnLine);
  312. IO.mapOptional("PointerAlignment", Style.PointerAlignment);
  313. IO.mapOptional("ReflowComments", Style.ReflowComments);
  314. IO.mapOptional("SortIncludes", Style.SortIncludes);
  315. IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
  316. IO.mapOptional("SpaceBeforeAssignmentOperators",
  317. Style.SpaceBeforeAssignmentOperators);
  318. IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
  319. IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
  320. IO.mapOptional("SpacesBeforeTrailingComments",
  321. Style.SpacesBeforeTrailingComments);
  322. IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
  323. IO.mapOptional("SpacesInContainerLiterals",
  324. Style.SpacesInContainerLiterals);
  325. IO.mapOptional("SpacesInCStyleCastParentheses",
  326. Style.SpacesInCStyleCastParentheses);
  327. IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
  328. IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
  329. IO.mapOptional("Standard", Style.Standard);
  330. IO.mapOptional("TabWidth", Style.TabWidth);
  331. IO.mapOptional("UseTab", Style.UseTab);
  332. }
  333. };
  334. template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
  335. static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
  336. IO.mapOptional("AfterClass", Wrapping.AfterClass);
  337. IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
  338. IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
  339. IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
  340. IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
  341. IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
  342. IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
  343. IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
  344. IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
  345. IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
  346. IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
  347. }
  348. };
  349. template <> struct MappingTraits<FormatStyle::IncludeCategory> {
  350. static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
  351. IO.mapOptional("Regex", Category.Regex);
  352. IO.mapOptional("Priority", Category.Priority);
  353. }
  354. };
  355. // Allows to read vector<FormatStyle> while keeping default values.
  356. // IO.getContext() should contain a pointer to the FormatStyle structure, that
  357. // will be used to get default values for missing keys.
  358. // If the first element has no Language specified, it will be treated as the
  359. // default one for the following elements.
  360. template <> struct DocumentListTraits<std::vector<FormatStyle>> {
  361. static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
  362. return Seq.size();
  363. }
  364. static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
  365. size_t Index) {
  366. if (Index >= Seq.size()) {
  367. assert(Index == Seq.size());
  368. FormatStyle Template;
  369. if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
  370. Template = Seq[0];
  371. } else {
  372. Template = *((const FormatStyle *)IO.getContext());
  373. Template.Language = FormatStyle::LK_None;
  374. }
  375. Seq.resize(Index + 1, Template);
  376. }
  377. return Seq[Index];
  378. }
  379. };
  380. } // namespace yaml
  381. } // namespace llvm
  382. namespace clang {
  383. namespace format {
  384. const std::error_category &getParseCategory() {
  385. static ParseErrorCategory C;
  386. return C;
  387. }
  388. std::error_code make_error_code(ParseError e) {
  389. return std::error_code(static_cast<int>(e), getParseCategory());
  390. }
  391. const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
  392. return "clang-format.parse_error";
  393. }
  394. std::string ParseErrorCategory::message(int EV) const {
  395. switch (static_cast<ParseError>(EV)) {
  396. case ParseError::Success:
  397. return "Success";
  398. case ParseError::Error:
  399. return "Invalid argument";
  400. case ParseError::Unsuitable:
  401. return "Unsuitable";
  402. }
  403. llvm_unreachable("unexpected parse error");
  404. }
  405. static FormatStyle expandPresets(const FormatStyle &Style) {
  406. if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
  407. return Style;
  408. FormatStyle Expanded = Style;
  409. Expanded.BraceWrapping = {false, false, false, false, false, false,
  410. false, false, false, false, false};
  411. switch (Style.BreakBeforeBraces) {
  412. case FormatStyle::BS_Linux:
  413. Expanded.BraceWrapping.AfterClass = true;
  414. Expanded.BraceWrapping.AfterFunction = true;
  415. Expanded.BraceWrapping.AfterNamespace = true;
  416. break;
  417. case FormatStyle::BS_Mozilla:
  418. Expanded.BraceWrapping.AfterClass = true;
  419. Expanded.BraceWrapping.AfterEnum = true;
  420. Expanded.BraceWrapping.AfterFunction = true;
  421. Expanded.BraceWrapping.AfterStruct = true;
  422. Expanded.BraceWrapping.AfterUnion = true;
  423. break;
  424. case FormatStyle::BS_Stroustrup:
  425. Expanded.BraceWrapping.AfterFunction = true;
  426. Expanded.BraceWrapping.BeforeCatch = true;
  427. Expanded.BraceWrapping.BeforeElse = true;
  428. break;
  429. case FormatStyle::BS_Allman:
  430. Expanded.BraceWrapping.AfterClass = true;
  431. Expanded.BraceWrapping.AfterControlStatement = true;
  432. Expanded.BraceWrapping.AfterEnum = true;
  433. Expanded.BraceWrapping.AfterFunction = true;
  434. Expanded.BraceWrapping.AfterNamespace = true;
  435. Expanded.BraceWrapping.AfterObjCDeclaration = true;
  436. Expanded.BraceWrapping.AfterStruct = true;
  437. Expanded.BraceWrapping.BeforeCatch = true;
  438. Expanded.BraceWrapping.BeforeElse = true;
  439. break;
  440. case FormatStyle::BS_GNU:
  441. Expanded.BraceWrapping = {true, true, true, true, true, true,
  442. true, true, true, true, true};
  443. break;
  444. case FormatStyle::BS_WebKit:
  445. Expanded.BraceWrapping.AfterFunction = true;
  446. break;
  447. default:
  448. break;
  449. }
  450. return Expanded;
  451. }
  452. FormatStyle getLLVMStyle() {
  453. FormatStyle LLVMStyle;
  454. LLVMStyle.Language = FormatStyle::LK_Cpp;
  455. LLVMStyle.AccessModifierOffset = -2;
  456. LLVMStyle.AlignEscapedNewlinesLeft = false;
  457. LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  458. LLVMStyle.AlignOperands = true;
  459. LLVMStyle.AlignTrailingComments = true;
  460. LLVMStyle.AlignConsecutiveAssignments = false;
  461. LLVMStyle.AlignConsecutiveDeclarations = false;
  462. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  463. LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  464. LLVMStyle.AllowShortBlocksOnASingleLine = false;
  465. LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
  466. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  467. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  468. LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
  469. LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
  470. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  471. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  472. LLVMStyle.BinPackParameters = true;
  473. LLVMStyle.BinPackArguments = true;
  474. LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  475. LLVMStyle.BreakBeforeTernaryOperators = true;
  476. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  477. LLVMStyle.BraceWrapping = {false, false, false, false, false, false,
  478. false, false, false, false, false};
  479. LLVMStyle.BreakAfterJavaFieldAnnotations = false;
  480. LLVMStyle.BreakConstructorInitializersBeforeComma = false;
  481. LLVMStyle.BreakStringLiterals = true;
  482. LLVMStyle.ColumnLimit = 80;
  483. LLVMStyle.CommentPragmas = "^ IWYU pragma:";
  484. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  485. LLVMStyle.ConstructorInitializerIndentWidth = 4;
  486. LLVMStyle.ContinuationIndentWidth = 4;
  487. LLVMStyle.Cpp11BracedListStyle = true;
  488. LLVMStyle.DerivePointerAlignment = false;
  489. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  490. LLVMStyle.ForEachMacros.push_back("foreach");
  491. LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
  492. LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
  493. LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
  494. {"^(<|\"(gtest|isl|json)/)", 3},
  495. {".*", 1}};
  496. LLVMStyle.IncludeIsMainRegex = "$";
  497. LLVMStyle.IndentCaseLabels = false;
  498. LLVMStyle.IndentWrappedFunctionNames = false;
  499. LLVMStyle.IndentWidth = 2;
  500. LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  501. LLVMStyle.JavaScriptWrapImports = true;
  502. LLVMStyle.TabWidth = 8;
  503. LLVMStyle.MaxEmptyLinesToKeep = 1;
  504. LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
  505. LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
  506. LLVMStyle.ObjCBlockIndentWidth = 2;
  507. LLVMStyle.ObjCSpaceAfterProperty = false;
  508. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  509. LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
  510. LLVMStyle.SpacesBeforeTrailingComments = 1;
  511. LLVMStyle.Standard = FormatStyle::LS_Cpp11;
  512. LLVMStyle.UseTab = FormatStyle::UT_Never;
  513. LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  514. LLVMStyle.ReflowComments = true;
  515. LLVMStyle.SpacesInParentheses = false;
  516. LLVMStyle.SpacesInSquareBrackets = false;
  517. LLVMStyle.SpaceInEmptyParentheses = false;
  518. LLVMStyle.SpacesInContainerLiterals = true;
  519. LLVMStyle.SpacesInCStyleCastParentheses = false;
  520. LLVMStyle.SpaceAfterCStyleCast = false;
  521. LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
  522. LLVMStyle.SpaceBeforeAssignmentOperators = true;
  523. LLVMStyle.SpacesInAngles = false;
  524. LLVMStyle.PenaltyBreakComment = 300;
  525. LLVMStyle.PenaltyBreakFirstLessLess = 120;
  526. LLVMStyle.PenaltyBreakString = 1000;
  527. LLVMStyle.PenaltyExcessCharacter = 1000000;
  528. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  529. LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
  530. LLVMStyle.DisableFormat = false;
  531. LLVMStyle.SortIncludes = true;
  532. return LLVMStyle;
  533. }
  534. FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
  535. FormatStyle GoogleStyle = getLLVMStyle();
  536. GoogleStyle.Language = Language;
  537. GoogleStyle.AccessModifierOffset = -1;
  538. GoogleStyle.AlignEscapedNewlinesLeft = true;
  539. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  540. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  541. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  542. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  543. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  544. GoogleStyle.DerivePointerAlignment = true;
  545. GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
  546. GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
  547. GoogleStyle.IndentCaseLabels = true;
  548. GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
  549. GoogleStyle.ObjCSpaceAfterProperty = false;
  550. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  551. GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
  552. GoogleStyle.SpacesBeforeTrailingComments = 2;
  553. GoogleStyle.Standard = FormatStyle::LS_Auto;
  554. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  555. GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
  556. if (Language == FormatStyle::LK_Java) {
  557. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  558. GoogleStyle.AlignOperands = false;
  559. GoogleStyle.AlignTrailingComments = false;
  560. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  561. GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
  562. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  563. GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  564. GoogleStyle.ColumnLimit = 100;
  565. GoogleStyle.SpaceAfterCStyleCast = true;
  566. GoogleStyle.SpacesBeforeTrailingComments = 1;
  567. } else if (Language == FormatStyle::LK_JavaScript) {
  568. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  569. GoogleStyle.AlignOperands = false;
  570. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  571. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  572. GoogleStyle.BreakBeforeTernaryOperators = false;
  573. GoogleStyle.CommentPragmas = "@(export|requirecss|return|see|visibility) ";
  574. GoogleStyle.MaxEmptyLinesToKeep = 3;
  575. GoogleStyle.NamespaceIndentation = FormatStyle::NI_All;
  576. GoogleStyle.SpacesInContainerLiterals = false;
  577. GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single;
  578. GoogleStyle.JavaScriptWrapImports = false;
  579. } else if (Language == FormatStyle::LK_Proto) {
  580. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  581. GoogleStyle.SpacesInContainerLiterals = false;
  582. }
  583. return GoogleStyle;
  584. }
  585. FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
  586. FormatStyle ChromiumStyle = getGoogleStyle(Language);
  587. if (Language == FormatStyle::LK_Java) {
  588. ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
  589. ChromiumStyle.BreakAfterJavaFieldAnnotations = true;
  590. ChromiumStyle.ContinuationIndentWidth = 8;
  591. ChromiumStyle.IndentWidth = 4;
  592. } else {
  593. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  594. ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  595. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  596. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  597. ChromiumStyle.BinPackParameters = false;
  598. ChromiumStyle.DerivePointerAlignment = false;
  599. }
  600. ChromiumStyle.SortIncludes = false;
  601. return ChromiumStyle;
  602. }
  603. FormatStyle getMozillaStyle() {
  604. FormatStyle MozillaStyle = getLLVMStyle();
  605. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  606. MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  607. MozillaStyle.AlwaysBreakAfterReturnType =
  608. FormatStyle::RTBS_TopLevelDefinitions;
  609. MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
  610. FormatStyle::DRTBS_TopLevel;
  611. MozillaStyle.AlwaysBreakTemplateDeclarations = true;
  612. MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  613. MozillaStyle.BreakConstructorInitializersBeforeComma = true;
  614. MozillaStyle.ConstructorInitializerIndentWidth = 2;
  615. MozillaStyle.ContinuationIndentWidth = 2;
  616. MozillaStyle.Cpp11BracedListStyle = false;
  617. MozillaStyle.IndentCaseLabels = true;
  618. MozillaStyle.ObjCSpaceAfterProperty = true;
  619. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  620. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  621. MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
  622. return MozillaStyle;
  623. }
  624. FormatStyle getWebKitStyle() {
  625. FormatStyle Style = getLLVMStyle();
  626. Style.AccessModifierOffset = -4;
  627. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  628. Style.AlignOperands = false;
  629. Style.AlignTrailingComments = false;
  630. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  631. Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
  632. Style.BreakConstructorInitializersBeforeComma = true;
  633. Style.Cpp11BracedListStyle = false;
  634. Style.ColumnLimit = 0;
  635. Style.IndentWidth = 4;
  636. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  637. Style.ObjCBlockIndentWidth = 4;
  638. Style.ObjCSpaceAfterProperty = true;
  639. Style.PointerAlignment = FormatStyle::PAS_Left;
  640. Style.Standard = FormatStyle::LS_Cpp03;
  641. return Style;
  642. }
  643. FormatStyle getGNUStyle() {
  644. FormatStyle Style = getLLVMStyle();
  645. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  646. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
  647. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  648. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  649. Style.BreakBeforeTernaryOperators = true;
  650. Style.Cpp11BracedListStyle = false;
  651. Style.ColumnLimit = 79;
  652. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  653. Style.Standard = FormatStyle::LS_Cpp03;
  654. return Style;
  655. }
  656. FormatStyle getNoStyle() {
  657. FormatStyle NoStyle = getLLVMStyle();
  658. NoStyle.DisableFormat = true;
  659. NoStyle.SortIncludes = false;
  660. return NoStyle;
  661. }
  662. bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
  663. FormatStyle *Style) {
  664. if (Name.equals_lower("llvm")) {
  665. *Style = getLLVMStyle();
  666. } else if (Name.equals_lower("chromium")) {
  667. *Style = getChromiumStyle(Language);
  668. } else if (Name.equals_lower("mozilla")) {
  669. *Style = getMozillaStyle();
  670. } else if (Name.equals_lower("google")) {
  671. *Style = getGoogleStyle(Language);
  672. } else if (Name.equals_lower("webkit")) {
  673. *Style = getWebKitStyle();
  674. } else if (Name.equals_lower("gnu")) {
  675. *Style = getGNUStyle();
  676. } else if (Name.equals_lower("none")) {
  677. *Style = getNoStyle();
  678. } else {
  679. return false;
  680. }
  681. Style->Language = Language;
  682. return true;
  683. }
  684. std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  685. assert(Style);
  686. FormatStyle::LanguageKind Language = Style->Language;
  687. assert(Language != FormatStyle::LK_None);
  688. if (Text.trim().empty())
  689. return make_error_code(ParseError::Error);
  690. std::vector<FormatStyle> Styles;
  691. llvm::yaml::Input Input(Text);
  692. // DocumentListTraits<vector<FormatStyle>> uses the context to get default
  693. // values for the fields, keys for which are missing from the configuration.
  694. // Mapping also uses the context to get the language to find the correct
  695. // base style.
  696. Input.setContext(Style);
  697. Input >> Styles;
  698. if (Input.error())
  699. return Input.error();
  700. for (unsigned i = 0; i < Styles.size(); ++i) {
  701. // Ensures that only the first configuration can skip the Language option.
  702. if (Styles[i].Language == FormatStyle::LK_None && i != 0)
  703. return make_error_code(ParseError::Error);
  704. // Ensure that each language is configured at most once.
  705. for (unsigned j = 0; j < i; ++j) {
  706. if (Styles[i].Language == Styles[j].Language) {
  707. DEBUG(llvm::dbgs()
  708. << "Duplicate languages in the config file on positions " << j
  709. << " and " << i << "\n");
  710. return make_error_code(ParseError::Error);
  711. }
  712. }
  713. }
  714. // Look for a suitable configuration starting from the end, so we can
  715. // find the configuration for the specific language first, and the default
  716. // configuration (which can only be at slot 0) after it.
  717. for (int i = Styles.size() - 1; i >= 0; --i) {
  718. if (Styles[i].Language == Language ||
  719. Styles[i].Language == FormatStyle::LK_None) {
  720. *Style = Styles[i];
  721. Style->Language = Language;
  722. return make_error_code(ParseError::Success);
  723. }
  724. }
  725. return make_error_code(ParseError::Unsuitable);
  726. }
  727. std::string configurationAsText(const FormatStyle &Style) {
  728. std::string Text;
  729. llvm::raw_string_ostream Stream(Text);
  730. llvm::yaml::Output Output(Stream);
  731. // We use the same mapping method for input and output, so we need a non-const
  732. // reference here.
  733. FormatStyle NonConstStyle = expandPresets(Style);
  734. Output << NonConstStyle;
  735. return Stream.str();
  736. }
  737. namespace {
  738. class Formatter : public TokenAnalyzer {
  739. public:
  740. Formatter(const Environment &Env, const FormatStyle &Style,
  741. bool *IncompleteFormat)
  742. : TokenAnalyzer(Env, Style), IncompleteFormat(IncompleteFormat) {}
  743. tooling::Replacements
  744. analyze(TokenAnnotator &Annotator,
  745. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  746. FormatTokenLexer &Tokens, tooling::Replacements &Result) override {
  747. deriveLocalStyle(AnnotatedLines);
  748. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  749. AnnotatedLines.end());
  750. if (Style.Language == FormatStyle::LK_JavaScript &&
  751. Style.JavaScriptQuotes != FormatStyle::JSQS_Leave)
  752. requoteJSStringLiteral(AnnotatedLines, Result);
  753. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  754. Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
  755. }
  756. Annotator.setCommentLineLevels(AnnotatedLines);
  757. WhitespaceManager Whitespaces(
  758. Env.getSourceManager(), Style,
  759. inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID())));
  760. ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
  761. Env.getSourceManager(), Whitespaces, Encoding,
  762. BinPackInconclusiveFunctions);
  763. UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(),
  764. IncompleteFormat)
  765. .format(AnnotatedLines);
  766. return Whitespaces.generateReplacements();
  767. }
  768. private:
  769. // If the last token is a double/single-quoted string literal, generates a
  770. // replacement with a single/double quoted string literal, re-escaping the
  771. // contents in the process.
  772. void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines,
  773. tooling::Replacements &Result) {
  774. for (AnnotatedLine *Line : Lines) {
  775. requoteJSStringLiteral(Line->Children, Result);
  776. if (!Line->Affected)
  777. continue;
  778. for (FormatToken *FormatTok = Line->First; FormatTok;
  779. FormatTok = FormatTok->Next) {
  780. StringRef Input = FormatTok->TokenText;
  781. if (FormatTok->Finalized || !FormatTok->isStringLiteral() ||
  782. // NB: testing for not starting with a double quote to avoid
  783. // breaking
  784. // `template strings`.
  785. (Style.JavaScriptQuotes == FormatStyle::JSQS_Single &&
  786. !Input.startswith("\"")) ||
  787. (Style.JavaScriptQuotes == FormatStyle::JSQS_Double &&
  788. !Input.startswith("\'")))
  789. continue;
  790. // Change start and end quote.
  791. bool IsSingle = Style.JavaScriptQuotes == FormatStyle::JSQS_Single;
  792. SourceLocation Start = FormatTok->Tok.getLocation();
  793. auto Replace = [&](SourceLocation Start, unsigned Length,
  794. StringRef ReplacementText) {
  795. Result.insert(tooling::Replacement(Env.getSourceManager(), Start,
  796. Length, ReplacementText));
  797. };
  798. Replace(Start, 1, IsSingle ? "'" : "\"");
  799. Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1,
  800. IsSingle ? "'" : "\"");
  801. // Escape internal quotes.
  802. size_t ColumnWidth = FormatTok->TokenText.size();
  803. bool Escaped = false;
  804. for (size_t i = 1; i < Input.size() - 1; i++) {
  805. switch (Input[i]) {
  806. case '\\':
  807. if (!Escaped && i + 1 < Input.size() &&
  808. ((IsSingle && Input[i + 1] == '"') ||
  809. (!IsSingle && Input[i + 1] == '\''))) {
  810. // Remove this \, it's escaping a " or ' that no longer needs
  811. // escaping
  812. ColumnWidth--;
  813. Replace(Start.getLocWithOffset(i), 1, "");
  814. continue;
  815. }
  816. Escaped = !Escaped;
  817. break;
  818. case '\"':
  819. case '\'':
  820. if (!Escaped && IsSingle == (Input[i] == '\'')) {
  821. // Escape the quote.
  822. Replace(Start.getLocWithOffset(i), 0, "\\");
  823. ColumnWidth++;
  824. }
  825. Escaped = false;
  826. break;
  827. default:
  828. Escaped = false;
  829. break;
  830. }
  831. }
  832. // For formatting, count the number of non-escaped single quotes in them
  833. // and adjust ColumnWidth to take the added escapes into account.
  834. // FIXME(martinprobst): this might conflict with code breaking a long
  835. // string literal (which clang-format doesn't do, yet). For that to
  836. // work, this code would have to modify TokenText directly.
  837. FormatTok->ColumnWidth = ColumnWidth;
  838. }
  839. }
  840. }
  841. static bool inputUsesCRLF(StringRef Text) {
  842. return Text.count('\r') * 2 > Text.count('\n');
  843. }
  844. bool
  845. hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  846. for (const AnnotatedLine *Line : Lines) {
  847. if (hasCpp03IncompatibleFormat(Line->Children))
  848. return true;
  849. for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
  850. if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
  851. if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
  852. return true;
  853. if (Tok->is(TT_TemplateCloser) &&
  854. Tok->Previous->is(TT_TemplateCloser))
  855. return true;
  856. }
  857. }
  858. }
  859. return false;
  860. }
  861. int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  862. int AlignmentDiff = 0;
  863. for (const AnnotatedLine *Line : Lines) {
  864. AlignmentDiff += countVariableAlignments(Line->Children);
  865. for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
  866. if (!Tok->is(TT_PointerOrReference))
  867. continue;
  868. bool SpaceBefore =
  869. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  870. bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
  871. Tok->Next->WhitespaceRange.getEnd();
  872. if (SpaceBefore && !SpaceAfter)
  873. ++AlignmentDiff;
  874. if (!SpaceBefore && SpaceAfter)
  875. --AlignmentDiff;
  876. }
  877. }
  878. return AlignmentDiff;
  879. }
  880. void
  881. deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  882. bool HasBinPackedFunction = false;
  883. bool HasOnePerLineFunction = false;
  884. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  885. if (!AnnotatedLines[i]->First->Next)
  886. continue;
  887. FormatToken *Tok = AnnotatedLines[i]->First->Next;
  888. while (Tok->Next) {
  889. if (Tok->PackingKind == PPK_BinPacked)
  890. HasBinPackedFunction = true;
  891. if (Tok->PackingKind == PPK_OnePerLine)
  892. HasOnePerLineFunction = true;
  893. Tok = Tok->Next;
  894. }
  895. }
  896. if (Style.DerivePointerAlignment)
  897. Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
  898. ? FormatStyle::PAS_Left
  899. : FormatStyle::PAS_Right;
  900. if (Style.Standard == FormatStyle::LS_Auto)
  901. Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
  902. ? FormatStyle::LS_Cpp11
  903. : FormatStyle::LS_Cpp03;
  904. BinPackInconclusiveFunctions =
  905. HasBinPackedFunction || !HasOnePerLineFunction;
  906. }
  907. bool BinPackInconclusiveFunctions;
  908. bool *IncompleteFormat;
  909. };
  910. // This class clean up the erroneous/redundant code around the given ranges in
  911. // file.
  912. class Cleaner : public TokenAnalyzer {
  913. public:
  914. Cleaner(const Environment &Env, const FormatStyle &Style)
  915. : TokenAnalyzer(Env, Style),
  916. DeletedTokens(FormatTokenLess(Env.getSourceManager())) {}
  917. // FIXME: eliminate unused parameters.
  918. tooling::Replacements
  919. analyze(TokenAnnotator &Annotator,
  920. SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  921. FormatTokenLexer &Tokens, tooling::Replacements &Result) override {
  922. // FIXME: in the current implementation the granularity of affected range
  923. // is an annotated line. However, this is not sufficient. Furthermore,
  924. // redundant code introduced by replacements does not necessarily
  925. // intercept with ranges of replacements that result in the redundancy.
  926. // To determine if some redundant code is actually introduced by
  927. // replacements(e.g. deletions), we need to come up with a more
  928. // sophisticated way of computing affected ranges.
  929. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  930. AnnotatedLines.end());
  931. checkEmptyNamespace(AnnotatedLines);
  932. for (auto &Line : AnnotatedLines) {
  933. if (Line->Affected) {
  934. cleanupRight(Line->First, tok::comma, tok::comma);
  935. cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma);
  936. cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace);
  937. cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace);
  938. }
  939. }
  940. return generateFixes();
  941. }
  942. private:
  943. bool containsOnlyComments(const AnnotatedLine &Line) {
  944. for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) {
  945. if (Tok->isNot(tok::comment))
  946. return false;
  947. }
  948. return true;
  949. }
  950. // Iterate through all lines and remove any empty (nested) namespaces.
  951. void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  952. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  953. auto &Line = *AnnotatedLines[i];
  954. if (Line.startsWith(tok::kw_namespace) ||
  955. Line.startsWith(tok::kw_inline, tok::kw_namespace)) {
  956. checkEmptyNamespace(AnnotatedLines, i, i);
  957. }
  958. }
  959. for (auto Line : DeletedLines) {
  960. FormatToken *Tok = AnnotatedLines[Line]->First;
  961. while (Tok) {
  962. deleteToken(Tok);
  963. Tok = Tok->Next;
  964. }
  965. }
  966. }
  967. // The function checks if the namespace, which starts from \p CurrentLine, and
  968. // its nested namespaces are empty and delete them if they are empty. It also
  969. // sets \p NewLine to the last line checked.
  970. // Returns true if the current namespace is empty.
  971. bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  972. unsigned CurrentLine, unsigned &NewLine) {
  973. unsigned InitLine = CurrentLine, End = AnnotatedLines.size();
  974. if (Style.BraceWrapping.AfterNamespace) {
  975. // If the left brace is in a new line, we should consume it first so that
  976. // it does not make the namespace non-empty.
  977. // FIXME: error handling if there is no left brace.
  978. if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) {
  979. NewLine = CurrentLine;
  980. return false;
  981. }
  982. } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) {
  983. return false;
  984. }
  985. while (++CurrentLine < End) {
  986. if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace))
  987. break;
  988. if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) ||
  989. AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline,
  990. tok::kw_namespace)) {
  991. if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine))
  992. return false;
  993. CurrentLine = NewLine;
  994. continue;
  995. }
  996. if (containsOnlyComments(*AnnotatedLines[CurrentLine]))
  997. continue;
  998. // If there is anything other than comments or nested namespaces in the
  999. // current namespace, the namespace cannot be empty.
  1000. NewLine = CurrentLine;
  1001. return false;
  1002. }
  1003. NewLine = CurrentLine;
  1004. if (CurrentLine >= End)
  1005. return false;
  1006. // Check if the empty namespace is actually affected by changed ranges.
  1007. if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange(
  1008. AnnotatedLines[InitLine]->First->Tok.getLocation(),
  1009. AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc())))
  1010. return false;
  1011. for (unsigned i = InitLine; i <= CurrentLine; ++i) {
  1012. DeletedLines.insert(i);
  1013. }
  1014. return true;
  1015. }
  1016. // Checks pairs {start, start->next},..., {end->previous, end} and deletes one
  1017. // of the token in the pair if the left token has \p LK token kind and the
  1018. // right token has \p RK token kind. If \p DeleteLeft is true, the left token
  1019. // is deleted on match; otherwise, the right token is deleted.
  1020. template <typename LeftKind, typename RightKind>
  1021. void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK,
  1022. bool DeleteLeft) {
  1023. auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * {
  1024. for (auto *Res = Tok.Next; Res; Res = Res->Next)
  1025. if (!Res->is(tok::comment) &&
  1026. DeletedTokens.find(Res) == DeletedTokens.end())
  1027. return Res;
  1028. return nullptr;
  1029. };
  1030. for (auto *Left = Start; Left;) {
  1031. auto *Right = NextNotDeleted(*Left);
  1032. if (!Right)
  1033. break;
  1034. if (Left->is(LK) && Right->is(RK)) {
  1035. deleteToken(DeleteLeft ? Left : Right);
  1036. // If the right token is deleted, we should keep the left token
  1037. // unchanged and pair it with the new right token.
  1038. if (!DeleteLeft)
  1039. continue;
  1040. }
  1041. Left = Right;
  1042. }
  1043. }
  1044. template <typename LeftKind, typename RightKind>
  1045. void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) {
  1046. cleanupPair(Start, LK, RK, /*DeleteLeft=*/true);
  1047. }
  1048. template <typename LeftKind, typename RightKind>
  1049. void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) {
  1050. cleanupPair(Start, LK, RK, /*DeleteLeft=*/false);
  1051. }
  1052. // Delete the given token.
  1053. inline void deleteToken(FormatToken *Tok) {
  1054. if (Tok)
  1055. DeletedTokens.insert(Tok);
  1056. }
  1057. tooling::Replacements generateFixes() {
  1058. tooling::Replacements Fixes;
  1059. std::vector<FormatToken *> Tokens;
  1060. std::copy(DeletedTokens.begin(), DeletedTokens.end(),
  1061. std::back_inserter(Tokens));
  1062. // Merge multiple continuous token deletions into one big deletion so that
  1063. // the number of replacements can be reduced. This makes computing affected
  1064. // ranges more efficient when we run reformat on the changed code.
  1065. unsigned Idx = 0;
  1066. while (Idx < Tokens.size()) {
  1067. unsigned St = Idx, End = Idx;
  1068. while ((End + 1) < Tokens.size() &&
  1069. Tokens[End]->Next == Tokens[End + 1]) {
  1070. End++;
  1071. }
  1072. auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(),
  1073. Tokens[End]->Tok.getEndLoc());
  1074. Fixes.insert(tooling::Replacement(Env.getSourceManager(), SR, ""));
  1075. Idx = End + 1;
  1076. }
  1077. return Fixes;
  1078. }
  1079. // Class for less-than inequality comparason for the set `RedundantTokens`.
  1080. // We store tokens in the order they appear in the translation unit so that
  1081. // we do not need to sort them in `generateFixes()`.
  1082. struct FormatTokenLess {
  1083. FormatTokenLess(const SourceManager &SM) : SM(SM) {}
  1084. bool operator()(const FormatToken *LHS, const FormatToken *RHS) const {
  1085. return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(),
  1086. RHS->Tok.getLocation());
  1087. }
  1088. const SourceManager &SM;
  1089. };
  1090. // Tokens to be deleted.
  1091. std::set<FormatToken *, FormatTokenLess> DeletedTokens;
  1092. // The line numbers of lines to be deleted.
  1093. std::set<unsigned> DeletedLines;
  1094. };
  1095. struct IncludeDirective {
  1096. StringRef Filename;
  1097. StringRef Text;
  1098. unsigned Offset;
  1099. int Category;
  1100. };
  1101. } // end anonymous namespace
  1102. // Determines whether 'Ranges' intersects with ('Start', 'End').
  1103. static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
  1104. unsigned End) {
  1105. for (auto Range : Ranges) {
  1106. if (Range.getOffset() < End &&
  1107. Range.getOffset() + Range.getLength() > Start)
  1108. return true;
  1109. }
  1110. return false;
  1111. }
  1112. // Sorts a block of includes given by 'Includes' alphabetically adding the
  1113. // necessary replacement to 'Replaces'. 'Includes' must be in strict source
  1114. // order.
  1115. static void sortCppIncludes(const FormatStyle &Style,
  1116. const SmallVectorImpl<IncludeDirective> &Includes,
  1117. ArrayRef<tooling::Range> Ranges, StringRef FileName,
  1118. tooling::Replacements &Replaces, unsigned *Cursor) {
  1119. if (!affectsRange(Ranges, Includes.front().Offset,
  1120. Includes.back().Offset + Includes.back().Text.size()))
  1121. return;
  1122. SmallVector<unsigned, 16> Indices;
  1123. for (unsigned i = 0, e = Includes.size(); i != e; ++i)
  1124. Indices.push_back(i);
  1125. std::stable_sort(
  1126. Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
  1127. return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
  1128. std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
  1129. });
  1130. // If the #includes are out of order, we generate a single replacement fixing
  1131. // the entire block. Otherwise, no replacement is generated.
  1132. if (std::is_sorted(Indices.begin(), Indices.end()))
  1133. return;
  1134. std::string result;
  1135. bool CursorMoved = false;
  1136. for (unsigned Index : Indices) {
  1137. if (!result.empty())
  1138. result += "\n";
  1139. result += Includes[Index].Text;
  1140. if (Cursor && !CursorMoved) {
  1141. unsigned Start = Includes[Index].Offset;
  1142. unsigned End = Start + Includes[Index].Text.size();
  1143. if (*Cursor >= Start && *Cursor < End) {
  1144. *Cursor = Includes.front().Offset + result.size() + *Cursor - End;
  1145. CursorMoved = true;
  1146. }
  1147. }
  1148. }
  1149. // Sorting #includes shouldn't change their total number of characters.
  1150. // This would otherwise mess up 'Ranges'.
  1151. assert(result.size() ==
  1152. Includes.back().Offset + Includes.back().Text.size() -
  1153. Includes.front().Offset);
  1154. Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
  1155. result.size(), result));
  1156. }
  1157. namespace {
  1158. // This class manages priorities of #include categories and calculates
  1159. // priorities for headers.
  1160. class IncludeCategoryManager {
  1161. public:
  1162. IncludeCategoryManager(const FormatStyle &Style, StringRef FileName)
  1163. : Style(Style), FileName(FileName) {
  1164. FileStem = llvm::sys::path::stem(FileName);
  1165. for (const auto &Category : Style.IncludeCategories)
  1166. CategoryRegexs.emplace_back(Category.Regex);
  1167. IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
  1168. FileName.endswith(".cpp") || FileName.endswith(".c++") ||
  1169. FileName.endswith(".cxx") || FileName.endswith(".m") ||
  1170. FileName.endswith(".mm");
  1171. }
  1172. // Returns the priority of the category which \p IncludeName belongs to.
  1173. // If \p CheckMainHeader is true and \p IncludeName is a main header, returns
  1174. // 0. Otherwise, returns the priority of the matching category or INT_MAX.
  1175. int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) {
  1176. int Ret = INT_MAX;
  1177. for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
  1178. if (CategoryRegexs[i].match(IncludeName)) {
  1179. Ret = Style.IncludeCategories[i].Priority;
  1180. break;
  1181. }
  1182. if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
  1183. Ret = 0;
  1184. return Ret;
  1185. }
  1186. private:
  1187. bool isMainHeader(StringRef IncludeName) const {
  1188. if (!IncludeName.startswith("\""))
  1189. return false;
  1190. StringRef HeaderStem =
  1191. llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
  1192. if (FileStem.startswith(HeaderStem)) {
  1193. llvm::Regex MainIncludeRegex(
  1194. (HeaderStem + Style.IncludeIsMainRegex).str());
  1195. if (MainIncludeRegex.match(FileStem))
  1196. return true;
  1197. }
  1198. return false;
  1199. }
  1200. const FormatStyle &Style;
  1201. bool IsMainFile;
  1202. StringRef FileName;
  1203. StringRef FileStem;
  1204. SmallVector<llvm::Regex, 4> CategoryRegexs;
  1205. };
  1206. const char IncludeRegexPattern[] =
  1207. R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
  1208. } // anonymous namespace
  1209. tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
  1210. ArrayRef<tooling::Range> Ranges,
  1211. StringRef FileName,
  1212. tooling::Replacements &Replaces,
  1213. unsigned *Cursor) {
  1214. unsigned Prev = 0;
  1215. unsigned SearchFrom = 0;
  1216. llvm::Regex IncludeRegex(IncludeRegexPattern);
  1217. SmallVector<StringRef, 4> Matches;
  1218. SmallVector<IncludeDirective, 16> IncludesInBlock;
  1219. // In compiled files, consider the first #include to be the main #include of
  1220. // the file if it is not a system #include. This ensures that the header
  1221. // doesn't have hidden dependencies
  1222. // (http://llvm.org/docs/CodingStandards.html#include-style).
  1223. //
  1224. // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
  1225. // cases where the first #include is unlikely to be the main header.
  1226. IncludeCategoryManager Categories(Style, FileName);
  1227. bool FirstIncludeBlock = true;
  1228. bool MainIncludeFound = false;
  1229. bool FormattingOff = false;
  1230. for (;;) {
  1231. auto Pos = Code.find('\n', SearchFrom);
  1232. StringRef Line =
  1233. Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
  1234. StringRef Trimmed = Line.trim();
  1235. if (Trimmed == "// clang-format off")
  1236. FormattingOff = true;
  1237. else if (Trimmed == "// clang-format on")
  1238. FormattingOff = false;
  1239. if (!FormattingOff && !Line.endswith("\\")) {
  1240. if (IncludeRegex.match(Line, &Matches)) {
  1241. StringRef IncludeName = Matches[2];
  1242. int Category = Categories.getIncludePriority(
  1243. IncludeName,
  1244. /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);
  1245. if (Category == 0)
  1246. MainIncludeFound = true;
  1247. IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
  1248. } else if (!IncludesInBlock.empty()) {
  1249. sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
  1250. Cursor);
  1251. IncludesInBlock.clear();
  1252. FirstIncludeBlock = false;
  1253. }
  1254. Prev = Pos + 1;
  1255. }
  1256. if (Pos == StringRef::npos || Pos + 1 == Code.size())
  1257. break;
  1258. SearchFrom = Pos + 1;
  1259. }
  1260. if (!IncludesInBlock.empty())
  1261. sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor);
  1262. return Replaces;
  1263. }
  1264. tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
  1265. ArrayRef<tooling::Range> Ranges,
  1266. StringRef FileName, unsigned *Cursor) {
  1267. tooling::Replacements Replaces;
  1268. if (!Style.SortIncludes)
  1269. return Replaces;
  1270. if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
  1271. return sortJavaScriptImports(Style, Code, Ranges, FileName);
  1272. sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
  1273. return Replaces;
  1274. }
  1275. template <typename T>
  1276. static llvm::Expected<tooling::Replacements>
  1277. processReplacements(T ProcessFunc, StringRef Code,
  1278. const tooling::Replacements &Replaces,
  1279. const FormatStyle &Style) {
  1280. if (Replaces.empty())
  1281. return tooling::Replacements();
  1282. auto NewCode = applyAllReplacements(Code, Replaces);
  1283. if (!NewCode)
  1284. return NewCode.takeError();
  1285. std::vector<tooling::Range> ChangedRanges =
  1286. tooling::calculateChangedRanges(Replaces);
  1287. StringRef FileName = Replaces.begin()->getFilePath();
  1288. tooling::Replacements FormatReplaces =
  1289. ProcessFunc(Style, *NewCode, ChangedRanges, FileName);
  1290. return mergeReplacements(Replaces, FormatReplaces);
  1291. }
  1292. llvm::Expected<tooling::Replacements>
  1293. formatReplacements(StringRef Code, const tooling::Replacements &Replaces,
  1294. const FormatStyle &Style) {
  1295. // We need to use lambda function here since there are two versions of
  1296. // `sortIncludes`.
  1297. auto SortIncludes = [](const FormatStyle &Style, StringRef Code,
  1298. std::vector<tooling::Range> Ranges,
  1299. StringRef FileName) -> tooling::Replacements {
  1300. return sortIncludes(Style, Code, Ranges, FileName);
  1301. };
  1302. auto SortedReplaces =
  1303. processReplacements(SortIncludes, Code, Replaces, Style);
  1304. if (!SortedReplaces)
  1305. return SortedReplaces.takeError();
  1306. // We need to use lambda function here since there are two versions of
  1307. // `reformat`.
  1308. auto Reformat = [](const FormatStyle &Style, StringRef Code,
  1309. std::vector<tooling::Range> Ranges,
  1310. StringRef FileName) -> tooling::Replacements {
  1311. return reformat(Style, Code, Ranges, FileName);
  1312. };
  1313. return processReplacements(Reformat, Code, *SortedReplaces, Style);
  1314. }
  1315. namespace {
  1316. inline bool isHeaderInsertion(const tooling::Replacement &Replace) {
  1317. return Replace.getOffset() == UINT_MAX &&
  1318. llvm::Regex(IncludeRegexPattern).match(Replace.getReplacementText());
  1319. }
  1320. void skipComments(Lexer &Lex, Token &Tok) {
  1321. while (Tok.is(tok::comment))
  1322. if (Lex.LexFromRawLexer(Tok))
  1323. return;
  1324. }
  1325. // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,
  1326. // \p Tok will be the token after this directive; otherwise, it can be any token
  1327. // after the given \p Tok (including \p Tok).
  1328. bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {
  1329. bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
  1330. Tok.is(tok::raw_identifier) &&
  1331. Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
  1332. Tok.is(tok::raw_identifier);
  1333. if (Matched)
  1334. Lex.LexFromRawLexer(Tok);
  1335. return Matched;
  1336. }
  1337. unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
  1338. StringRef Code,
  1339. const FormatStyle &Style) {
  1340. std::unique_ptr<Environment> Env =
  1341. Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{});
  1342. const SourceManager &SourceMgr = Env->getSourceManager();
  1343. Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr,
  1344. getFormattingLangOpts(Style));
  1345. Token Tok;
  1346. // Get the first token.
  1347. Lex.LexFromRawLexer(Tok);
  1348. skipComments(Lex, Tok);
  1349. unsigned AfterComments = SourceMgr.getFileOffset(Tok.getLocation());
  1350. if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
  1351. skipComments(Lex, Tok);
  1352. if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))
  1353. return SourceMgr.getFileOffset(Tok.getLocation());
  1354. }
  1355. return AfterComments;
  1356. }
  1357. // FIXME: we also need to insert a '\n' at the end of the code if we have an
  1358. // insertion with offset Code.size(), and there is no '\n' at the end of the
  1359. // code.
  1360. // FIXME: do not insert headers into conditional #include blocks, e.g. #includes
  1361. // surrounded by compile condition "#if...".
  1362. // FIXME: insert empty lines between newly created blocks.
  1363. tooling::Replacements
  1364. fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
  1365. const FormatStyle &Style) {
  1366. if (Style.Language != FormatStyle::LanguageKind::LK_Cpp)
  1367. return Replaces;
  1368. tooling::Replacements HeaderInsertions;
  1369. for (const auto &R : Replaces) {
  1370. if (isHeaderInsertion(R))
  1371. HeaderInsertions.insert(R);
  1372. else if (R.getOffset() == UINT_MAX)
  1373. llvm::errs() << "Insertions other than header #include insertion are "
  1374. "not supported! "
  1375. << R.getReplacementText() << "\n";
  1376. }
  1377. if (HeaderInsertions.empty())
  1378. return Replaces;
  1379. tooling::Replacements Result;
  1380. std::set_difference(Replaces.begin(), Replaces.end(),
  1381. HeaderInsertions.begin(), HeaderInsertions.end(),
  1382. std::inserter(Result, Result.begin()));
  1383. llvm::Regex IncludeRegex(IncludeRegexPattern);
  1384. llvm::Regex DefineRegex(R"(^[\t\ ]*#[\t\ ]*define[\t\ ]*[^\\]*$)");
  1385. SmallVector<StringRef, 4> Matches;
  1386. StringRef FileName = Replaces.begin()->getFilePath();
  1387. IncludeCategoryManager Categories(Style, FileName);
  1388. // Record the offset of the end of the last include in each category.
  1389. std::map<int, int> CategoryEndOffsets;
  1390. // All possible priorities.
  1391. // Add 0 for main header and INT_MAX for headers that are not in any category.
  1392. std::set<int> Priorities = {0, INT_MAX};
  1393. for (const auto &Category : Style.IncludeCategories)
  1394. Priorities.insert(Category.Priority);
  1395. int FirstIncludeOffset = -1;
  1396. // All new headers should be inserted after this offset.
  1397. unsigned MinInsertOffset =
  1398. getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style);
  1399. StringRef TrimmedCode = Code.drop_front(MinInsertOffset);
  1400. SmallVector<StringRef, 32> Lines;
  1401. TrimmedCode.split(Lines, '\n');
  1402. unsigned Offset = MinInsertOffset;
  1403. unsigned NextLineOffset;
  1404. std::set<StringRef> ExistingIncludes;
  1405. for (auto Line : Lines) {
  1406. NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
  1407. if (IncludeRegex.match(Line, &Matches)) {
  1408. StringRef IncludeName = Matches[2];
  1409. ExistingIncludes.insert(IncludeName);
  1410. int Category = Categories.getIncludePriority(
  1411. IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
  1412. CategoryEndOffsets[Category] = NextLineOffset;
  1413. if (FirstIncludeOffset < 0)
  1414. FirstIncludeOffset = Offset;
  1415. }
  1416. Offset = NextLineOffset;
  1417. }
  1418. // Populate CategoryEndOfssets:
  1419. // - Ensure that CategoryEndOffset[Highest] is always populated.
  1420. // - If CategoryEndOffset[Priority] isn't set, use the next higher value that
  1421. // is set, up to CategoryEndOffset[Highest].
  1422. auto Highest = Priorities.begin();
  1423. if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
  1424. if (FirstIncludeOffset >= 0)
  1425. CategoryEndOffsets[*Highest] = FirstIncludeOffset;
  1426. else
  1427. CategoryEndOffsets[*Highest] = MinInsertOffset;
  1428. }
  1429. // By this point, CategoryEndOffset[Highest] is always set appropriately:
  1430. // - to an appropriate location before/after existing #includes, or
  1431. // - to right after the header guard, or
  1432. // - to the beginning of the file.
  1433. for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
  1434. if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
  1435. CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
  1436. for (const auto &R : HeaderInsertions) {
  1437. auto IncludeDirective = R.getReplacementText();
  1438. bool Matched = IncludeRegex.match(IncludeDirective, &Matches);
  1439. assert(Matched && "Header insertion replacement must have replacement text "
  1440. "'#include ...'");
  1441. (void)Matched;
  1442. auto IncludeName = Matches[2];
  1443. if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) {
  1444. DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName
  1445. << "\n");
  1446. continue;
  1447. }
  1448. int Category =
  1449. Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
  1450. Offset = CategoryEndOffsets[Category];
  1451. std::string NewInclude = !IncludeDirective.endswith("\n")
  1452. ? (IncludeDirective + "\n").str()
  1453. : IncludeDirective.str();
  1454. Result.insert(tooling::Replacement(FileName, Offset, 0, NewInclude));
  1455. }
  1456. return Result;
  1457. }
  1458. } // anonymous namespace
  1459. llvm::Expected<tooling::Replacements>
  1460. cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces,
  1461. const FormatStyle &Style) {
  1462. // We need to use lambda function here since there are two versions of
  1463. // `cleanup`.
  1464. auto Cleanup = [](const FormatStyle &Style, StringRef Code,
  1465. std::vector<tooling::Range> Ranges,
  1466. StringRef FileName) -> tooling::Replacements {
  1467. return cleanup(Style, Code, Ranges, FileName);
  1468. };
  1469. // Make header insertion replacements insert new headers into correct blocks.
  1470. tooling::Replacements NewReplaces =
  1471. fixCppIncludeInsertions(Code, Replaces, Style);
  1472. return processReplacements(Cleanup, Code, NewReplaces, Style);
  1473. }
  1474. tooling::Replacements reformat(const FormatStyle &Style, SourceManager &SM,
  1475. FileID ID, ArrayRef<CharSourceRange> Ranges,
  1476. bool *IncompleteFormat) {
  1477. FormatStyle Expanded = expandPresets(Style);
  1478. if (Expanded.DisableFormat)
  1479. return tooling::Replacements();
  1480. Environment Env(SM, ID, Ranges);
  1481. Formatter Format(Env, Expanded, IncompleteFormat);
  1482. return Format.process();
  1483. }
  1484. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1485. ArrayRef<tooling::Range> Ranges,
  1486. StringRef FileName, bool *IncompleteFormat) {
  1487. FormatStyle Expanded = expandPresets(Style);
  1488. if (Expanded.DisableFormat)
  1489. return tooling::Replacements();
  1490. std::unique_ptr<Environment> Env =
  1491. Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
  1492. Formatter Format(*Env, Expanded, IncompleteFormat);
  1493. return Format.process();
  1494. }
  1495. tooling::Replacements cleanup(const FormatStyle &Style, SourceManager &SM,
  1496. FileID ID, ArrayRef<CharSourceRange> Ranges) {
  1497. Environment Env(SM, ID, Ranges);
  1498. Cleaner Clean(Env, Style);
  1499. return Clean.process();
  1500. }
  1501. tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code,
  1502. ArrayRef<tooling::Range> Ranges,
  1503. StringRef FileName) {
  1504. std::unique_ptr<Environment> Env =
  1505. Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
  1506. Cleaner Clean(*Env, Style);
  1507. return Clean.process();
  1508. }
  1509. LangOptions getFormattingLangOpts(const FormatStyle &Style) {
  1510. LangOptions LangOpts;
  1511. LangOpts.CPlusPlus = 1;
  1512. LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1513. LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1514. LangOpts.LineComment = 1;
  1515. bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
  1516. LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
  1517. LangOpts.Bool = 1;
  1518. LangOpts.ObjC1 = 1;
  1519. LangOpts.ObjC2 = 1;
  1520. LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
  1521. LangOpts.DeclSpecKeyword = 1; // To get __declspec.
  1522. return LangOpts;
  1523. }
  1524. const char *StyleOptionHelpDescription =
  1525. "Coding style, currently supports:\n"
  1526. " LLVM, Google, Chromium, Mozilla, WebKit.\n"
  1527. "Use -style=file to load style configuration from\n"
  1528. ".clang-format file located in one of the parent\n"
  1529. "directories of the source file (or current\n"
  1530. "directory for stdin).\n"
  1531. "Use -style=\"{key: value, ...}\" to set specific\n"
  1532. "parameters, e.g.:\n"
  1533. " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
  1534. static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
  1535. if (FileName.endswith(".java"))
  1536. return FormatStyle::LK_Java;
  1537. if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts"))
  1538. return FormatStyle::LK_JavaScript; // JavaScript or TypeScript.
  1539. if (FileName.endswith_lower(".proto") ||
  1540. FileName.endswith_lower(".protodevel"))
  1541. return FormatStyle::LK_Proto;
  1542. if (FileName.endswith_lower(".td"))
  1543. return FormatStyle::LK_TableGen;
  1544. return FormatStyle::LK_Cpp;
  1545. }
  1546. FormatStyle getStyle(StringRef StyleName, StringRef FileName,
  1547. StringRef FallbackStyle, vfs::FileSystem *FS) {
  1548. if (!FS) {
  1549. FS = vfs::getRealFileSystem().get();
  1550. }
  1551. FormatStyle Style = getLLVMStyle();
  1552. Style.Language = getLanguageByFileName(FileName);
  1553. if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
  1554. llvm::errs() << "Invalid fallback style \"" << FallbackStyle
  1555. << "\" using LLVM style\n";
  1556. return Style;
  1557. }
  1558. if (StyleName.startswith("{")) {
  1559. // Parse YAML/JSON style from the command line.
  1560. if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
  1561. llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
  1562. << FallbackStyle << " style\n";
  1563. }
  1564. return Style;
  1565. }
  1566. if (!StyleName.equals_lower("file")) {
  1567. if (!getPredefinedStyle(StyleName, Style.Language, &Style))
  1568. llvm::errs() << "Invalid value for -style, using " << FallbackStyle
  1569. << " style\n";
  1570. return Style;
  1571. }
  1572. // Look for .clang-format/_clang-format file in the file's parent directories.
  1573. SmallString<128> UnsuitableConfigFiles;
  1574. SmallString<128> Path(FileName);
  1575. llvm::sys::fs::make_absolute(Path);
  1576. for (StringRef Directory = Path; !Directory.empty();
  1577. Directory = llvm::sys::path::parent_path(Directory)) {
  1578. auto Status = FS->status(Directory);
  1579. if (!Status ||
  1580. Status->getType() != llvm::sys::fs::file_type::directory_file) {
  1581. continue;
  1582. }
  1583. SmallString<128> ConfigFile(Directory);
  1584. llvm::sys::path::append(ConfigFile, ".clang-format");
  1585. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1586. Status = FS->status(ConfigFile.str());
  1587. bool IsFile =
  1588. Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
  1589. if (!IsFile) {
  1590. // Try _clang-format too, since dotfiles are not commonly used on Windows.
  1591. ConfigFile = Directory;
  1592. llvm::sys::path::append(ConfigFile, "_clang-format");
  1593. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1594. Status = FS->status(ConfigFile.str());
  1595. IsFile = Status &&
  1596. (Status->getType() == llvm::sys::fs::file_type::regular_file);
  1597. }
  1598. if (IsFile) {
  1599. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
  1600. FS->getBufferForFile(ConfigFile.str());
  1601. if (std::error_code EC = Text.getError()) {
  1602. llvm::errs() << EC.message() << "\n";
  1603. break;
  1604. }
  1605. if (std::error_code ec =
  1606. parseConfiguration(Text.get()->getBuffer(), &Style)) {
  1607. if (ec == ParseError::Unsuitable) {
  1608. if (!UnsuitableConfigFiles.empty())
  1609. UnsuitableConfigFiles.append(", ");
  1610. UnsuitableConfigFiles.append(ConfigFile);
  1611. continue;
  1612. }
  1613. llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
  1614. << "\n";
  1615. break;
  1616. }
  1617. DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
  1618. return Style;
  1619. }
  1620. }
  1621. if (!UnsuitableConfigFiles.empty()) {
  1622. llvm::errs() << "Configuration file(s) do(es) not support "
  1623. << getLanguageName(Style.Language) << ": "
  1624. << UnsuitableConfigFiles << "\n";
  1625. }
  1626. return Style;
  1627. }
  1628. } // namespace format
  1629. } // namespace clang