Format.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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 "ContinuationIndenter.h"
  17. #include "TokenAnnotator.h"
  18. #include "UnwrappedLineFormatter.h"
  19. #include "UnwrappedLineParser.h"
  20. #include "WhitespaceManager.h"
  21. #include "clang/Basic/Diagnostic.h"
  22. #include "clang/Basic/DiagnosticOptions.h"
  23. #include "clang/Basic/SourceManager.h"
  24. #include "clang/Lex/Lexer.h"
  25. #include "llvm/ADT/STLExtras.h"
  26. #include "llvm/Support/Allocator.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/Path.h"
  29. #include "llvm/Support/Regex.h"
  30. #include "llvm/Support/YAMLTraits.h"
  31. #include <queue>
  32. #include <string>
  33. #define DEBUG_TYPE "format-formatter"
  34. using clang::format::FormatStyle;
  35. LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
  36. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
  37. namespace llvm {
  38. namespace yaml {
  39. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
  40. static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
  41. IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
  42. IO.enumCase(Value, "Java", FormatStyle::LK_Java);
  43. IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
  44. IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
  45. }
  46. };
  47. template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
  48. static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
  49. IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
  50. IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
  51. IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
  52. IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
  53. IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
  54. }
  55. };
  56. template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
  57. static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
  58. IO.enumCase(Value, "Never", FormatStyle::UT_Never);
  59. IO.enumCase(Value, "false", FormatStyle::UT_Never);
  60. IO.enumCase(Value, "Always", FormatStyle::UT_Always);
  61. IO.enumCase(Value, "true", FormatStyle::UT_Always);
  62. IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
  63. }
  64. };
  65. template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
  66. static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
  67. IO.enumCase(Value, "None", FormatStyle::SFS_None);
  68. IO.enumCase(Value, "false", FormatStyle::SFS_None);
  69. IO.enumCase(Value, "All", FormatStyle::SFS_All);
  70. IO.enumCase(Value, "true", FormatStyle::SFS_All);
  71. IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
  72. IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
  73. }
  74. };
  75. template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
  76. static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
  77. IO.enumCase(Value, "All", FormatStyle::BOS_All);
  78. IO.enumCase(Value, "true", FormatStyle::BOS_All);
  79. IO.enumCase(Value, "None", FormatStyle::BOS_None);
  80. IO.enumCase(Value, "false", FormatStyle::BOS_None);
  81. IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
  82. }
  83. };
  84. template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
  85. static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
  86. IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
  87. IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
  88. IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
  89. IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
  90. IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
  91. IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
  92. IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
  93. IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
  94. }
  95. };
  96. template <>
  97. struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
  98. static void
  99. enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
  100. IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
  101. IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
  102. IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
  103. // For backward compatibility.
  104. IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
  105. IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
  106. }
  107. };
  108. template <>
  109. struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
  110. static void enumeration(IO &IO,
  111. FormatStyle::NamespaceIndentationKind &Value) {
  112. IO.enumCase(Value, "None", FormatStyle::NI_None);
  113. IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
  114. IO.enumCase(Value, "All", FormatStyle::NI_All);
  115. }
  116. };
  117. template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
  118. static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
  119. IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
  120. IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
  121. IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
  122. // For backward compatibility.
  123. IO.enumCase(Value, "true", FormatStyle::BAS_Align);
  124. IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
  125. }
  126. };
  127. template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
  128. static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
  129. IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
  130. IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
  131. IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
  132. // For backward compatibility.
  133. IO.enumCase(Value, "true", FormatStyle::PAS_Left);
  134. IO.enumCase(Value, "false", FormatStyle::PAS_Right);
  135. }
  136. };
  137. template <>
  138. struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
  139. static void enumeration(IO &IO,
  140. FormatStyle::SpaceBeforeParensOptions &Value) {
  141. IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
  142. IO.enumCase(Value, "ControlStatements",
  143. FormatStyle::SBPO_ControlStatements);
  144. IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
  145. // For backward compatibility.
  146. IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
  147. IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
  148. }
  149. };
  150. template <> struct MappingTraits<FormatStyle> {
  151. static void mapping(IO &IO, FormatStyle &Style) {
  152. // When reading, read the language first, we need it for getPredefinedStyle.
  153. IO.mapOptional("Language", Style.Language);
  154. if (IO.outputting()) {
  155. StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
  156. "Mozilla", "WebKit", "GNU"};
  157. ArrayRef<StringRef> Styles(StylesArray);
  158. for (size_t i = 0, e = Styles.size(); i < e; ++i) {
  159. StringRef StyleName(Styles[i]);
  160. FormatStyle PredefinedStyle;
  161. if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
  162. Style == PredefinedStyle) {
  163. IO.mapOptional("# BasedOnStyle", StyleName);
  164. break;
  165. }
  166. }
  167. } else {
  168. StringRef BasedOnStyle;
  169. IO.mapOptional("BasedOnStyle", BasedOnStyle);
  170. if (!BasedOnStyle.empty()) {
  171. FormatStyle::LanguageKind OldLanguage = Style.Language;
  172. FormatStyle::LanguageKind Language =
  173. ((FormatStyle *)IO.getContext())->Language;
  174. if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
  175. IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
  176. return;
  177. }
  178. Style.Language = OldLanguage;
  179. }
  180. }
  181. // For backward compatibility.
  182. if (!IO.outputting()) {
  183. IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
  184. IO.mapOptional("IndentFunctionDeclarationAfterType",
  185. Style.IndentWrappedFunctionNames);
  186. IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
  187. IO.mapOptional("SpaceAfterControlStatementKeyword",
  188. Style.SpaceBeforeParens);
  189. }
  190. IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
  191. IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
  192. IO.mapOptional("AlignConsecutiveAssignments",
  193. Style.AlignConsecutiveAssignments);
  194. IO.mapOptional("AlignConsecutiveDeclarations",
  195. Style.AlignConsecutiveDeclarations);
  196. IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
  197. IO.mapOptional("AlignOperands", Style.AlignOperands);
  198. IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
  199. IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
  200. Style.AllowAllParametersOfDeclarationOnNextLine);
  201. IO.mapOptional("AllowShortBlocksOnASingleLine",
  202. Style.AllowShortBlocksOnASingleLine);
  203. IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
  204. Style.AllowShortCaseLabelsOnASingleLine);
  205. IO.mapOptional("AllowShortFunctionsOnASingleLine",
  206. Style.AllowShortFunctionsOnASingleLine);
  207. IO.mapOptional("AllowShortIfStatementsOnASingleLine",
  208. Style.AllowShortIfStatementsOnASingleLine);
  209. IO.mapOptional("AllowShortLoopsOnASingleLine",
  210. Style.AllowShortLoopsOnASingleLine);
  211. IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
  212. Style.AlwaysBreakAfterDefinitionReturnType);
  213. IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
  214. Style.AlwaysBreakBeforeMultilineStrings);
  215. IO.mapOptional("AlwaysBreakTemplateDeclarations",
  216. Style.AlwaysBreakTemplateDeclarations);
  217. IO.mapOptional("BinPackArguments", Style.BinPackArguments);
  218. IO.mapOptional("BinPackParameters", Style.BinPackParameters);
  219. IO.mapOptional("BraceWrapping", Style.BraceWrapping);
  220. IO.mapOptional("BreakBeforeBinaryOperators",
  221. Style.BreakBeforeBinaryOperators);
  222. IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
  223. IO.mapOptional("BreakBeforeTernaryOperators",
  224. Style.BreakBeforeTernaryOperators);
  225. IO.mapOptional("BreakConstructorInitializersBeforeComma",
  226. Style.BreakConstructorInitializersBeforeComma);
  227. IO.mapOptional("ColumnLimit", Style.ColumnLimit);
  228. IO.mapOptional("CommentPragmas", Style.CommentPragmas);
  229. IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
  230. Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
  231. IO.mapOptional("ConstructorInitializerIndentWidth",
  232. Style.ConstructorInitializerIndentWidth);
  233. IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
  234. IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
  235. IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
  236. IO.mapOptional("DisableFormat", Style.DisableFormat);
  237. IO.mapOptional("ExperimentalAutoDetectBinPacking",
  238. Style.ExperimentalAutoDetectBinPacking);
  239. IO.mapOptional("ForEachMacros", Style.ForEachMacros);
  240. IO.mapOptional("IncludeCategories", Style.IncludeCategories);
  241. IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
  242. IO.mapOptional("IndentWidth", Style.IndentWidth);
  243. IO.mapOptional("IndentWrappedFunctionNames",
  244. Style.IndentWrappedFunctionNames);
  245. IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
  246. Style.KeepEmptyLinesAtTheStartOfBlocks);
  247. IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
  248. IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
  249. IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
  250. IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
  251. IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
  252. IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
  253. IO.mapOptional("ObjCSpaceBeforeProtocolList",
  254. Style.ObjCSpaceBeforeProtocolList);
  255. IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
  256. Style.PenaltyBreakBeforeFirstCallParameter);
  257. IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
  258. IO.mapOptional("PenaltyBreakFirstLessLess",
  259. Style.PenaltyBreakFirstLessLess);
  260. IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
  261. IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
  262. IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
  263. Style.PenaltyReturnTypeOnItsOwnLine);
  264. IO.mapOptional("SortIncludes", Style.SortIncludes);
  265. IO.mapOptional("PointerAlignment", Style.PointerAlignment);
  266. IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
  267. IO.mapOptional("SpaceBeforeAssignmentOperators",
  268. Style.SpaceBeforeAssignmentOperators);
  269. IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
  270. IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
  271. IO.mapOptional("SpacesBeforeTrailingComments",
  272. Style.SpacesBeforeTrailingComments);
  273. IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
  274. IO.mapOptional("SpacesInContainerLiterals",
  275. Style.SpacesInContainerLiterals);
  276. IO.mapOptional("SpacesInCStyleCastParentheses",
  277. Style.SpacesInCStyleCastParentheses);
  278. IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
  279. IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
  280. IO.mapOptional("Standard", Style.Standard);
  281. IO.mapOptional("TabWidth", Style.TabWidth);
  282. IO.mapOptional("UseTab", Style.UseTab);
  283. }
  284. };
  285. template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
  286. static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
  287. IO.mapOptional("AfterClass", Wrapping.AfterClass);
  288. IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
  289. IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
  290. IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
  291. IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
  292. IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
  293. IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
  294. IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
  295. IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
  296. IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
  297. IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
  298. }
  299. };
  300. template <> struct MappingTraits<FormatStyle::IncludeCategory> {
  301. static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
  302. IO.mapOptional("Regex", Category.Regex);
  303. IO.mapOptional("Priority", Category.Priority);
  304. }
  305. };
  306. // Allows to read vector<FormatStyle> while keeping default values.
  307. // IO.getContext() should contain a pointer to the FormatStyle structure, that
  308. // will be used to get default values for missing keys.
  309. // If the first element has no Language specified, it will be treated as the
  310. // default one for the following elements.
  311. template <> struct DocumentListTraits<std::vector<FormatStyle>> {
  312. static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
  313. return Seq.size();
  314. }
  315. static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
  316. size_t Index) {
  317. if (Index >= Seq.size()) {
  318. assert(Index == Seq.size());
  319. FormatStyle Template;
  320. if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
  321. Template = Seq[0];
  322. } else {
  323. Template = *((const FormatStyle *)IO.getContext());
  324. Template.Language = FormatStyle::LK_None;
  325. }
  326. Seq.resize(Index + 1, Template);
  327. }
  328. return Seq[Index];
  329. }
  330. };
  331. } // namespace yaml
  332. } // namespace llvm
  333. namespace clang {
  334. namespace format {
  335. const std::error_category &getParseCategory() {
  336. static ParseErrorCategory C;
  337. return C;
  338. }
  339. std::error_code make_error_code(ParseError e) {
  340. return std::error_code(static_cast<int>(e), getParseCategory());
  341. }
  342. const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
  343. return "clang-format.parse_error";
  344. }
  345. std::string ParseErrorCategory::message(int EV) const {
  346. switch (static_cast<ParseError>(EV)) {
  347. case ParseError::Success:
  348. return "Success";
  349. case ParseError::Error:
  350. return "Invalid argument";
  351. case ParseError::Unsuitable:
  352. return "Unsuitable";
  353. }
  354. llvm_unreachable("unexpected parse error");
  355. }
  356. static FormatStyle expandPresets(const FormatStyle &Style) {
  357. if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
  358. return Style;
  359. FormatStyle Expanded = Style;
  360. Expanded.BraceWrapping = {false, false, false, false, false, false,
  361. false, false, false, false, false};
  362. switch (Style.BreakBeforeBraces) {
  363. case FormatStyle::BS_Linux:
  364. Expanded.BraceWrapping.AfterClass = true;
  365. Expanded.BraceWrapping.AfterFunction = true;
  366. Expanded.BraceWrapping.AfterNamespace = true;
  367. Expanded.BraceWrapping.BeforeElse = true;
  368. break;
  369. case FormatStyle::BS_Mozilla:
  370. Expanded.BraceWrapping.AfterClass = true;
  371. Expanded.BraceWrapping.AfterEnum = true;
  372. Expanded.BraceWrapping.AfterFunction = true;
  373. Expanded.BraceWrapping.AfterStruct = true;
  374. Expanded.BraceWrapping.AfterUnion = true;
  375. break;
  376. case FormatStyle::BS_Stroustrup:
  377. Expanded.BraceWrapping.AfterFunction = true;
  378. Expanded.BraceWrapping.BeforeCatch = true;
  379. Expanded.BraceWrapping.BeforeElse = true;
  380. break;
  381. case FormatStyle::BS_Allman:
  382. Expanded.BraceWrapping.AfterClass = true;
  383. Expanded.BraceWrapping.AfterControlStatement = true;
  384. Expanded.BraceWrapping.AfterEnum = true;
  385. Expanded.BraceWrapping.AfterFunction = true;
  386. Expanded.BraceWrapping.AfterNamespace = true;
  387. Expanded.BraceWrapping.AfterObjCDeclaration = true;
  388. Expanded.BraceWrapping.AfterStruct = true;
  389. Expanded.BraceWrapping.BeforeCatch = true;
  390. Expanded.BraceWrapping.BeforeElse = true;
  391. break;
  392. case FormatStyle::BS_GNU:
  393. Expanded.BraceWrapping = {true, true, true, true, true, true,
  394. true, true, true, true, true};
  395. break;
  396. case FormatStyle::BS_WebKit:
  397. Expanded.BraceWrapping.AfterFunction = true;
  398. Expanded.BraceWrapping.BeforeElse = true;
  399. break;
  400. default:
  401. break;
  402. }
  403. return Expanded;
  404. }
  405. FormatStyle getLLVMStyle() {
  406. FormatStyle LLVMStyle;
  407. LLVMStyle.Language = FormatStyle::LK_Cpp;
  408. LLVMStyle.AccessModifierOffset = -2;
  409. LLVMStyle.AlignEscapedNewlinesLeft = false;
  410. LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
  411. LLVMStyle.AlignOperands = true;
  412. LLVMStyle.AlignTrailingComments = true;
  413. LLVMStyle.AlignConsecutiveAssignments = false;
  414. LLVMStyle.AlignConsecutiveDeclarations = false;
  415. LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
  416. LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  417. LLVMStyle.AllowShortBlocksOnASingleLine = false;
  418. LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
  419. LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
  420. LLVMStyle.AllowShortLoopsOnASingleLine = false;
  421. LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
  422. LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
  423. LLVMStyle.AlwaysBreakTemplateDeclarations = false;
  424. LLVMStyle.BinPackParameters = true;
  425. LLVMStyle.BinPackArguments = true;
  426. LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
  427. LLVMStyle.BreakBeforeTernaryOperators = true;
  428. LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
  429. LLVMStyle.BraceWrapping = {false, false, false, false, false, false,
  430. false, false, false, false, false};
  431. LLVMStyle.BreakConstructorInitializersBeforeComma = false;
  432. LLVMStyle.BreakAfterJavaFieldAnnotations = false;
  433. LLVMStyle.ColumnLimit = 80;
  434. LLVMStyle.CommentPragmas = "^ IWYU pragma:";
  435. LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
  436. LLVMStyle.ConstructorInitializerIndentWidth = 4;
  437. LLVMStyle.ContinuationIndentWidth = 4;
  438. LLVMStyle.Cpp11BracedListStyle = true;
  439. LLVMStyle.DerivePointerAlignment = false;
  440. LLVMStyle.ExperimentalAutoDetectBinPacking = false;
  441. LLVMStyle.ForEachMacros.push_back("foreach");
  442. LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
  443. LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
  444. LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
  445. {"^(<|\"(gtest|isl|json)/)", 3},
  446. {".*", 1}};
  447. LLVMStyle.IndentCaseLabels = false;
  448. LLVMStyle.IndentWrappedFunctionNames = false;
  449. LLVMStyle.IndentWidth = 2;
  450. LLVMStyle.TabWidth = 8;
  451. LLVMStyle.MaxEmptyLinesToKeep = 1;
  452. LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
  453. LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
  454. LLVMStyle.ObjCBlockIndentWidth = 2;
  455. LLVMStyle.ObjCSpaceAfterProperty = false;
  456. LLVMStyle.ObjCSpaceBeforeProtocolList = true;
  457. LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
  458. LLVMStyle.SpacesBeforeTrailingComments = 1;
  459. LLVMStyle.Standard = FormatStyle::LS_Cpp11;
  460. LLVMStyle.UseTab = FormatStyle::UT_Never;
  461. LLVMStyle.SpacesInParentheses = false;
  462. LLVMStyle.SpacesInSquareBrackets = false;
  463. LLVMStyle.SpaceInEmptyParentheses = false;
  464. LLVMStyle.SpacesInContainerLiterals = true;
  465. LLVMStyle.SpacesInCStyleCastParentheses = false;
  466. LLVMStyle.SpaceAfterCStyleCast = false;
  467. LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
  468. LLVMStyle.SpaceBeforeAssignmentOperators = true;
  469. LLVMStyle.SpacesInAngles = false;
  470. LLVMStyle.PenaltyBreakComment = 300;
  471. LLVMStyle.PenaltyBreakFirstLessLess = 120;
  472. LLVMStyle.PenaltyBreakString = 1000;
  473. LLVMStyle.PenaltyExcessCharacter = 1000000;
  474. LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
  475. LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
  476. LLVMStyle.DisableFormat = false;
  477. LLVMStyle.SortIncludes = true;
  478. return LLVMStyle;
  479. }
  480. FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
  481. FormatStyle GoogleStyle = getLLVMStyle();
  482. GoogleStyle.Language = Language;
  483. GoogleStyle.AccessModifierOffset = -1;
  484. GoogleStyle.AlignEscapedNewlinesLeft = true;
  485. GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
  486. GoogleStyle.AllowShortLoopsOnASingleLine = true;
  487. GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
  488. GoogleStyle.AlwaysBreakTemplateDeclarations = true;
  489. GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
  490. GoogleStyle.DerivePointerAlignment = true;
  491. GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
  492. GoogleStyle.IndentCaseLabels = true;
  493. GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
  494. GoogleStyle.ObjCSpaceAfterProperty = false;
  495. GoogleStyle.ObjCSpaceBeforeProtocolList = false;
  496. GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
  497. GoogleStyle.SpacesBeforeTrailingComments = 2;
  498. GoogleStyle.Standard = FormatStyle::LS_Auto;
  499. GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  500. GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
  501. if (Language == FormatStyle::LK_Java) {
  502. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  503. GoogleStyle.AlignOperands = false;
  504. GoogleStyle.AlignTrailingComments = false;
  505. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  506. GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
  507. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  508. GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
  509. GoogleStyle.ColumnLimit = 100;
  510. GoogleStyle.SpaceAfterCStyleCast = true;
  511. GoogleStyle.SpacesBeforeTrailingComments = 1;
  512. } else if (Language == FormatStyle::LK_JavaScript) {
  513. GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
  514. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  515. GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
  516. GoogleStyle.BreakBeforeTernaryOperators = false;
  517. GoogleStyle.MaxEmptyLinesToKeep = 3;
  518. GoogleStyle.SpacesInContainerLiterals = false;
  519. } else if (Language == FormatStyle::LK_Proto) {
  520. GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  521. GoogleStyle.SpacesInContainerLiterals = false;
  522. }
  523. return GoogleStyle;
  524. }
  525. FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
  526. FormatStyle ChromiumStyle = getGoogleStyle(Language);
  527. if (Language == FormatStyle::LK_Java) {
  528. ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
  529. ChromiumStyle.BreakAfterJavaFieldAnnotations = true;
  530. ChromiumStyle.ContinuationIndentWidth = 8;
  531. ChromiumStyle.IndentWidth = 4;
  532. } else {
  533. ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  534. ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  535. ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
  536. ChromiumStyle.AllowShortLoopsOnASingleLine = false;
  537. ChromiumStyle.BinPackParameters = false;
  538. ChromiumStyle.DerivePointerAlignment = false;
  539. }
  540. return ChromiumStyle;
  541. }
  542. FormatStyle getMozillaStyle() {
  543. FormatStyle MozillaStyle = getLLVMStyle();
  544. MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
  545. MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  546. MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
  547. FormatStyle::DRTBS_TopLevel;
  548. MozillaStyle.AlwaysBreakTemplateDeclarations = true;
  549. MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
  550. MozillaStyle.BreakConstructorInitializersBeforeComma = true;
  551. MozillaStyle.ConstructorInitializerIndentWidth = 2;
  552. MozillaStyle.ContinuationIndentWidth = 2;
  553. MozillaStyle.Cpp11BracedListStyle = false;
  554. MozillaStyle.IndentCaseLabels = true;
  555. MozillaStyle.ObjCSpaceAfterProperty = true;
  556. MozillaStyle.ObjCSpaceBeforeProtocolList = false;
  557. MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
  558. MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
  559. return MozillaStyle;
  560. }
  561. FormatStyle getWebKitStyle() {
  562. FormatStyle Style = getLLVMStyle();
  563. Style.AccessModifierOffset = -4;
  564. Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
  565. Style.AlignOperands = false;
  566. Style.AlignTrailingComments = false;
  567. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  568. Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
  569. Style.BreakConstructorInitializersBeforeComma = true;
  570. Style.Cpp11BracedListStyle = false;
  571. Style.ColumnLimit = 0;
  572. Style.IndentWidth = 4;
  573. Style.NamespaceIndentation = FormatStyle::NI_Inner;
  574. Style.ObjCBlockIndentWidth = 4;
  575. Style.ObjCSpaceAfterProperty = true;
  576. Style.PointerAlignment = FormatStyle::PAS_Left;
  577. Style.Standard = FormatStyle::LS_Cpp03;
  578. return Style;
  579. }
  580. FormatStyle getGNUStyle() {
  581. FormatStyle Style = getLLVMStyle();
  582. Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
  583. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  584. Style.BreakBeforeBraces = FormatStyle::BS_GNU;
  585. Style.BreakBeforeTernaryOperators = true;
  586. Style.Cpp11BracedListStyle = false;
  587. Style.ColumnLimit = 79;
  588. Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
  589. Style.Standard = FormatStyle::LS_Cpp03;
  590. return Style;
  591. }
  592. FormatStyle getNoStyle() {
  593. FormatStyle NoStyle = getLLVMStyle();
  594. NoStyle.DisableFormat = true;
  595. NoStyle.SortIncludes = false;
  596. return NoStyle;
  597. }
  598. bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
  599. FormatStyle *Style) {
  600. if (Name.equals_lower("llvm")) {
  601. *Style = getLLVMStyle();
  602. } else if (Name.equals_lower("chromium")) {
  603. *Style = getChromiumStyle(Language);
  604. } else if (Name.equals_lower("mozilla")) {
  605. *Style = getMozillaStyle();
  606. } else if (Name.equals_lower("google")) {
  607. *Style = getGoogleStyle(Language);
  608. } else if (Name.equals_lower("webkit")) {
  609. *Style = getWebKitStyle();
  610. } else if (Name.equals_lower("gnu")) {
  611. *Style = getGNUStyle();
  612. } else if (Name.equals_lower("none")) {
  613. *Style = getNoStyle();
  614. } else {
  615. return false;
  616. }
  617. Style->Language = Language;
  618. return true;
  619. }
  620. std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
  621. assert(Style);
  622. FormatStyle::LanguageKind Language = Style->Language;
  623. assert(Language != FormatStyle::LK_None);
  624. if (Text.trim().empty())
  625. return make_error_code(ParseError::Error);
  626. std::vector<FormatStyle> Styles;
  627. llvm::yaml::Input Input(Text);
  628. // DocumentListTraits<vector<FormatStyle>> uses the context to get default
  629. // values for the fields, keys for which are missing from the configuration.
  630. // Mapping also uses the context to get the language to find the correct
  631. // base style.
  632. Input.setContext(Style);
  633. Input >> Styles;
  634. if (Input.error())
  635. return Input.error();
  636. for (unsigned i = 0; i < Styles.size(); ++i) {
  637. // Ensures that only the first configuration can skip the Language option.
  638. if (Styles[i].Language == FormatStyle::LK_None && i != 0)
  639. return make_error_code(ParseError::Error);
  640. // Ensure that each language is configured at most once.
  641. for (unsigned j = 0; j < i; ++j) {
  642. if (Styles[i].Language == Styles[j].Language) {
  643. DEBUG(llvm::dbgs()
  644. << "Duplicate languages in the config file on positions " << j
  645. << " and " << i << "\n");
  646. return make_error_code(ParseError::Error);
  647. }
  648. }
  649. }
  650. // Look for a suitable configuration starting from the end, so we can
  651. // find the configuration for the specific language first, and the default
  652. // configuration (which can only be at slot 0) after it.
  653. for (int i = Styles.size() - 1; i >= 0; --i) {
  654. if (Styles[i].Language == Language ||
  655. Styles[i].Language == FormatStyle::LK_None) {
  656. *Style = Styles[i];
  657. Style->Language = Language;
  658. return make_error_code(ParseError::Success);
  659. }
  660. }
  661. return make_error_code(ParseError::Unsuitable);
  662. }
  663. std::string configurationAsText(const FormatStyle &Style) {
  664. std::string Text;
  665. llvm::raw_string_ostream Stream(Text);
  666. llvm::yaml::Output Output(Stream);
  667. // We use the same mapping method for input and output, so we need a non-const
  668. // reference here.
  669. FormatStyle NonConstStyle = expandPresets(Style);
  670. Output << NonConstStyle;
  671. return Stream.str();
  672. }
  673. namespace {
  674. class FormatTokenLexer {
  675. public:
  676. FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style,
  677. encoding::Encoding Encoding)
  678. : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
  679. LessStashed(false), Column(0), TrailingWhitespace(0),
  680. SourceMgr(SourceMgr), ID(ID), Style(Style),
  681. IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
  682. Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
  683. MacroBlockBeginRegex(Style.MacroBlockBegin),
  684. MacroBlockEndRegex(Style.MacroBlockEnd) {
  685. Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
  686. getFormattingLangOpts(Style)));
  687. Lex->SetKeepWhitespaceMode(true);
  688. for (const std::string &ForEachMacro : Style.ForEachMacros)
  689. ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
  690. std::sort(ForEachMacros.begin(), ForEachMacros.end());
  691. }
  692. ArrayRef<FormatToken *> lex() {
  693. assert(Tokens.empty());
  694. assert(FirstInLineIndex == 0);
  695. do {
  696. Tokens.push_back(getNextToken());
  697. if (Style.Language == FormatStyle::LK_JavaScript)
  698. tryParseJSRegexLiteral();
  699. tryMergePreviousTokens();
  700. if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
  701. FirstInLineIndex = Tokens.size() - 1;
  702. } while (Tokens.back()->Tok.isNot(tok::eof));
  703. return Tokens;
  704. }
  705. const AdditionalKeywords &getKeywords() { return Keywords; }
  706. private:
  707. void tryMergePreviousTokens() {
  708. if (tryMerge_TMacro())
  709. return;
  710. if (tryMergeConflictMarkers())
  711. return;
  712. if (tryMergeLessLess())
  713. return;
  714. if (Style.Language == FormatStyle::LK_JavaScript) {
  715. if (tryMergeTemplateString())
  716. return;
  717. static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
  718. static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
  719. tok::equal};
  720. static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
  721. tok::greaterequal};
  722. static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
  723. // FIXME: Investigate what token type gives the correct operator priority.
  724. if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
  725. return;
  726. if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
  727. return;
  728. if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
  729. return;
  730. if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
  731. return;
  732. }
  733. }
  734. bool tryMergeLessLess() {
  735. // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
  736. if (Tokens.size() < 3)
  737. return false;
  738. bool FourthTokenIsLess = false;
  739. if (Tokens.size() > 3)
  740. FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
  741. auto First = Tokens.end() - 3;
  742. if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
  743. First[0]->isNot(tok::less) || FourthTokenIsLess)
  744. return false;
  745. // Only merge if there currently is no whitespace between the two "<".
  746. if (First[1]->WhitespaceRange.getBegin() !=
  747. First[1]->WhitespaceRange.getEnd())
  748. return false;
  749. First[0]->Tok.setKind(tok::lessless);
  750. First[0]->TokenText = "<<";
  751. First[0]->ColumnWidth += 1;
  752. Tokens.erase(Tokens.end() - 2);
  753. return true;
  754. }
  755. bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) {
  756. if (Tokens.size() < Kinds.size())
  757. return false;
  758. SmallVectorImpl<FormatToken *>::const_iterator First =
  759. Tokens.end() - Kinds.size();
  760. if (!First[0]->is(Kinds[0]))
  761. return false;
  762. unsigned AddLength = 0;
  763. for (unsigned i = 1; i < Kinds.size(); ++i) {
  764. if (!First[i]->is(Kinds[i]) ||
  765. First[i]->WhitespaceRange.getBegin() !=
  766. First[i]->WhitespaceRange.getEnd())
  767. return false;
  768. AddLength += First[i]->TokenText.size();
  769. }
  770. Tokens.resize(Tokens.size() - Kinds.size() + 1);
  771. First[0]->TokenText = StringRef(First[0]->TokenText.data(),
  772. First[0]->TokenText.size() + AddLength);
  773. First[0]->ColumnWidth += AddLength;
  774. First[0]->Type = NewType;
  775. return true;
  776. }
  777. // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
  778. bool precedesOperand(FormatToken *Tok) {
  779. // NB: This is not entirely correct, as an r_paren can introduce an operand
  780. // location in e.g. `if (foo) /bar/.exec(...);`. That is a rare enough
  781. // corner case to not matter in practice, though.
  782. return Tok->isOneOf(tok::period, tok::l_paren, tok::comma, tok::l_brace,
  783. tok::r_brace, tok::l_square, tok::semi, tok::exclaim,
  784. tok::colon, tok::question, tok::tilde) ||
  785. Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw,
  786. tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void,
  787. tok::kw_typeof, Keywords.kw_instanceof,
  788. Keywords.kw_in) ||
  789. Tok->isBinaryOperator();
  790. }
  791. bool canPrecedeRegexLiteral(FormatToken *Prev) {
  792. if (!Prev)
  793. return true;
  794. // Regex literals can only follow after prefix unary operators, not after
  795. // postfix unary operators. If the '++' is followed by a non-operand
  796. // introducing token, the slash here is the operand and not the start of a
  797. // regex.
  798. if (Prev->isOneOf(tok::plusplus, tok::minusminus))
  799. return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3]));
  800. // The previous token must introduce an operand location where regex
  801. // literals can occur.
  802. if (!precedesOperand(Prev))
  803. return false;
  804. return true;
  805. }
  806. // Tries to parse a JavaScript Regex literal starting at the current token,
  807. // if that begins with a slash and is in a location where JavaScript allows
  808. // regex literals. Changes the current token to a regex literal and updates
  809. // its text if successful.
  810. void tryParseJSRegexLiteral() {
  811. FormatToken *RegexToken = Tokens.back();
  812. if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
  813. return;
  814. FormatToken *Prev = nullptr;
  815. for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
  816. // NB: Because previous pointers are not initialized yet, this cannot use
  817. // Token.getPreviousNonComment.
  818. if ((*I)->isNot(tok::comment)) {
  819. Prev = *I;
  820. break;
  821. }
  822. }
  823. if (!canPrecedeRegexLiteral(Prev))
  824. return;
  825. // 'Manually' lex ahead in the current file buffer.
  826. const char *Offset = Lex->getBufferLocation();
  827. const char *RegexBegin = Offset - RegexToken->TokenText.size();
  828. StringRef Buffer = Lex->getBuffer();
  829. bool InCharacterClass = false;
  830. bool HaveClosingSlash = false;
  831. for (; !HaveClosingSlash && Offset != Buffer.end(); ++Offset) {
  832. // Regular expressions are terminated with a '/', which can only be
  833. // escaped using '\' or a character class between '[' and ']'.
  834. // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5.
  835. switch (*Offset) {
  836. case '\\':
  837. // Skip the escaped character.
  838. ++Offset;
  839. break;
  840. case '[':
  841. InCharacterClass = true;
  842. break;
  843. case ']':
  844. InCharacterClass = false;
  845. break;
  846. case '/':
  847. if (!InCharacterClass)
  848. HaveClosingSlash = true;
  849. break;
  850. }
  851. }
  852. RegexToken->Type = TT_RegexLiteral;
  853. // Treat regex literals like other string_literals.
  854. RegexToken->Tok.setKind(tok::string_literal);
  855. RegexToken->TokenText = StringRef(RegexBegin, Offset - RegexBegin);
  856. RegexToken->ColumnWidth = RegexToken->TokenText.size();
  857. resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset)));
  858. }
  859. bool tryMergeTemplateString() {
  860. if (Tokens.size() < 2)
  861. return false;
  862. FormatToken *EndBacktick = Tokens.back();
  863. // Backticks get lexed as tok::unknown tokens. If a template string contains
  864. // a comment start, it gets lexed as a tok::comment, or tok::unknown if
  865. // unterminated.
  866. if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
  867. tok::char_constant, tok::unknown))
  868. return false;
  869. size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
  870. // Unknown token that's not actually a backtick, or a comment that doesn't
  871. // contain a backtick.
  872. if (CommentBacktickPos == StringRef::npos)
  873. return false;
  874. unsigned TokenCount = 0;
  875. bool IsMultiline = false;
  876. unsigned EndColumnInFirstLine =
  877. EndBacktick->OriginalColumn + EndBacktick->ColumnWidth;
  878. for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) {
  879. ++TokenCount;
  880. if (I[0]->IsMultiline)
  881. IsMultiline = true;
  882. // If there was a preceding template string, this must be the start of a
  883. // template string, not the end.
  884. if (I[0]->is(TT_TemplateString))
  885. return false;
  886. if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") {
  887. // Keep track of the rhs offset of the last token to wrap across lines -
  888. // its the rhs offset of the first line of the template string, used to
  889. // determine its width.
  890. if (I[0]->IsMultiline)
  891. EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth;
  892. // If the token has newlines, the token before it (if it exists) is the
  893. // rhs end of the previous line.
  894. if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) {
  895. EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth;
  896. IsMultiline = true;
  897. }
  898. continue;
  899. }
  900. Tokens.resize(Tokens.size() - TokenCount);
  901. Tokens.back()->Type = TT_TemplateString;
  902. const char *EndOffset =
  903. EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
  904. if (CommentBacktickPos != 0) {
  905. // If the backtick was not the first character (e.g. in a comment),
  906. // re-lex after the backtick position.
  907. SourceLocation Loc = EndBacktick->Tok.getLocation();
  908. resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
  909. }
  910. Tokens.back()->TokenText =
  911. StringRef(Tokens.back()->TokenText.data(),
  912. EndOffset - Tokens.back()->TokenText.data());
  913. unsigned EndOriginalColumn = EndBacktick->OriginalColumn;
  914. if (EndOriginalColumn == 0) {
  915. SourceLocation Loc = EndBacktick->Tok.getLocation();
  916. EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc);
  917. }
  918. // If the ` is further down within the token (e.g. in a comment).
  919. EndOriginalColumn += CommentBacktickPos;
  920. if (IsMultiline) {
  921. // ColumnWidth is from backtick to last token in line.
  922. // LastLineColumnWidth is 0 to backtick.
  923. // x = `some content
  924. // until here`;
  925. Tokens.back()->ColumnWidth =
  926. EndColumnInFirstLine - Tokens.back()->OriginalColumn;
  927. // +1 for the ` itself.
  928. Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1;
  929. Tokens.back()->IsMultiline = true;
  930. } else {
  931. // Token simply spans from start to end, +1 for the ` itself.
  932. Tokens.back()->ColumnWidth =
  933. EndOriginalColumn - Tokens.back()->OriginalColumn + 1;
  934. }
  935. return true;
  936. }
  937. return false;
  938. }
  939. bool tryMerge_TMacro() {
  940. if (Tokens.size() < 4)
  941. return false;
  942. FormatToken *Last = Tokens.back();
  943. if (!Last->is(tok::r_paren))
  944. return false;
  945. FormatToken *String = Tokens[Tokens.size() - 2];
  946. if (!String->is(tok::string_literal) || String->IsMultiline)
  947. return false;
  948. if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
  949. return false;
  950. FormatToken *Macro = Tokens[Tokens.size() - 4];
  951. if (Macro->TokenText != "_T")
  952. return false;
  953. const char *Start = Macro->TokenText.data();
  954. const char *End = Last->TokenText.data() + Last->TokenText.size();
  955. String->TokenText = StringRef(Start, End - Start);
  956. String->IsFirst = Macro->IsFirst;
  957. String->LastNewlineOffset = Macro->LastNewlineOffset;
  958. String->WhitespaceRange = Macro->WhitespaceRange;
  959. String->OriginalColumn = Macro->OriginalColumn;
  960. String->ColumnWidth = encoding::columnWidthWithTabs(
  961. String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
  962. String->NewlinesBefore = Macro->NewlinesBefore;
  963. String->HasUnescapedNewline = Macro->HasUnescapedNewline;
  964. Tokens.pop_back();
  965. Tokens.pop_back();
  966. Tokens.pop_back();
  967. Tokens.back() = String;
  968. return true;
  969. }
  970. bool tryMergeConflictMarkers() {
  971. if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
  972. return false;
  973. // Conflict lines look like:
  974. // <marker> <text from the vcs>
  975. // For example:
  976. // >>>>>>> /file/in/file/system at revision 1234
  977. //
  978. // We merge all tokens in a line that starts with a conflict marker
  979. // into a single token with a special token type that the unwrapped line
  980. // parser will use to correctly rebuild the underlying code.
  981. FileID ID;
  982. // Get the position of the first token in the line.
  983. unsigned FirstInLineOffset;
  984. std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
  985. Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
  986. StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
  987. // Calculate the offset of the start of the current line.
  988. auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
  989. if (LineOffset == StringRef::npos) {
  990. LineOffset = 0;
  991. } else {
  992. ++LineOffset;
  993. }
  994. auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
  995. StringRef LineStart;
  996. if (FirstSpace == StringRef::npos) {
  997. LineStart = Buffer.substr(LineOffset);
  998. } else {
  999. LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
  1000. }
  1001. TokenType Type = TT_Unknown;
  1002. if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
  1003. Type = TT_ConflictStart;
  1004. } else if (LineStart == "|||||||" || LineStart == "=======" ||
  1005. LineStart == "====") {
  1006. Type = TT_ConflictAlternative;
  1007. } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
  1008. Type = TT_ConflictEnd;
  1009. }
  1010. if (Type != TT_Unknown) {
  1011. FormatToken *Next = Tokens.back();
  1012. Tokens.resize(FirstInLineIndex + 1);
  1013. // We do not need to build a complete token here, as we will skip it
  1014. // during parsing anyway (as we must not touch whitespace around conflict
  1015. // markers).
  1016. Tokens.back()->Type = Type;
  1017. Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
  1018. Tokens.push_back(Next);
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. FormatToken *getStashedToken() {
  1024. // Create a synthesized second '>' or '<' token.
  1025. Token Tok = FormatTok->Tok;
  1026. StringRef TokenText = FormatTok->TokenText;
  1027. unsigned OriginalColumn = FormatTok->OriginalColumn;
  1028. FormatTok = new (Allocator.Allocate()) FormatToken;
  1029. FormatTok->Tok = Tok;
  1030. SourceLocation TokLocation =
  1031. FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
  1032. FormatTok->Tok.setLocation(TokLocation);
  1033. FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
  1034. FormatTok->TokenText = TokenText;
  1035. FormatTok->ColumnWidth = 1;
  1036. FormatTok->OriginalColumn = OriginalColumn + 1;
  1037. return FormatTok;
  1038. }
  1039. FormatToken *getNextToken() {
  1040. if (GreaterStashed) {
  1041. GreaterStashed = false;
  1042. return getStashedToken();
  1043. }
  1044. if (LessStashed) {
  1045. LessStashed = false;
  1046. return getStashedToken();
  1047. }
  1048. FormatTok = new (Allocator.Allocate()) FormatToken;
  1049. readRawToken(*FormatTok);
  1050. SourceLocation WhitespaceStart =
  1051. FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
  1052. FormatTok->IsFirst = IsFirstToken;
  1053. IsFirstToken = false;
  1054. // Consume and record whitespace until we find a significant token.
  1055. unsigned WhitespaceLength = TrailingWhitespace;
  1056. while (FormatTok->Tok.is(tok::unknown)) {
  1057. StringRef Text = FormatTok->TokenText;
  1058. auto EscapesNewline = [&](int pos) {
  1059. // A '\r' here is just part of '\r\n'. Skip it.
  1060. if (pos >= 0 && Text[pos] == '\r')
  1061. --pos;
  1062. // See whether there is an odd number of '\' before this.
  1063. unsigned count = 0;
  1064. for (; pos >= 0; --pos, ++count)
  1065. if (Text[pos] != '\\')
  1066. break;
  1067. return count & 1;
  1068. };
  1069. // FIXME: This miscounts tok:unknown tokens that are not just
  1070. // whitespace, e.g. a '`' character.
  1071. for (int i = 0, e = Text.size(); i != e; ++i) {
  1072. switch (Text[i]) {
  1073. case '\n':
  1074. ++FormatTok->NewlinesBefore;
  1075. FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
  1076. FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
  1077. Column = 0;
  1078. break;
  1079. case '\r':
  1080. FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
  1081. Column = 0;
  1082. break;
  1083. case '\f':
  1084. case '\v':
  1085. Column = 0;
  1086. break;
  1087. case ' ':
  1088. ++Column;
  1089. break;
  1090. case '\t':
  1091. Column += Style.TabWidth - Column % Style.TabWidth;
  1092. break;
  1093. case '\\':
  1094. if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
  1095. FormatTok->Type = TT_ImplicitStringLiteral;
  1096. break;
  1097. default:
  1098. FormatTok->Type = TT_ImplicitStringLiteral;
  1099. break;
  1100. }
  1101. }
  1102. if (FormatTok->is(TT_ImplicitStringLiteral))
  1103. break;
  1104. WhitespaceLength += FormatTok->Tok.getLength();
  1105. readRawToken(*FormatTok);
  1106. }
  1107. // In case the token starts with escaped newlines, we want to
  1108. // take them into account as whitespace - this pattern is quite frequent
  1109. // in macro definitions.
  1110. // FIXME: Add a more explicit test.
  1111. while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
  1112. FormatTok->TokenText[1] == '\n') {
  1113. ++FormatTok->NewlinesBefore;
  1114. WhitespaceLength += 2;
  1115. FormatTok->LastNewlineOffset = 2;
  1116. Column = 0;
  1117. FormatTok->TokenText = FormatTok->TokenText.substr(2);
  1118. }
  1119. FormatTok->WhitespaceRange = SourceRange(
  1120. WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
  1121. FormatTok->OriginalColumn = Column;
  1122. TrailingWhitespace = 0;
  1123. if (FormatTok->Tok.is(tok::comment)) {
  1124. // FIXME: Add the trimmed whitespace to Column.
  1125. StringRef UntrimmedText = FormatTok->TokenText;
  1126. FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
  1127. TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
  1128. } else if (FormatTok->Tok.is(tok::raw_identifier)) {
  1129. IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
  1130. FormatTok->Tok.setIdentifierInfo(&Info);
  1131. FormatTok->Tok.setKind(Info.getTokenID());
  1132. if (Style.Language == FormatStyle::LK_Java &&
  1133. FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
  1134. FormatTok->Tok.setKind(tok::identifier);
  1135. FormatTok->Tok.setIdentifierInfo(nullptr);
  1136. }
  1137. } else if (FormatTok->Tok.is(tok::greatergreater)) {
  1138. FormatTok->Tok.setKind(tok::greater);
  1139. FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
  1140. GreaterStashed = true;
  1141. } else if (FormatTok->Tok.is(tok::lessless)) {
  1142. FormatTok->Tok.setKind(tok::less);
  1143. FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
  1144. LessStashed = true;
  1145. }
  1146. // Now FormatTok is the next non-whitespace token.
  1147. StringRef Text = FormatTok->TokenText;
  1148. size_t FirstNewlinePos = Text.find('\n');
  1149. if (FirstNewlinePos == StringRef::npos) {
  1150. // FIXME: ColumnWidth actually depends on the start column, we need to
  1151. // take this into account when the token is moved.
  1152. FormatTok->ColumnWidth =
  1153. encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
  1154. Column += FormatTok->ColumnWidth;
  1155. } else {
  1156. FormatTok->IsMultiline = true;
  1157. // FIXME: ColumnWidth actually depends on the start column, we need to
  1158. // take this into account when the token is moved.
  1159. FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
  1160. Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
  1161. // The last line of the token always starts in column 0.
  1162. // Thus, the length can be precomputed even in the presence of tabs.
  1163. FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
  1164. Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
  1165. Encoding);
  1166. Column = FormatTok->LastLineColumnWidth;
  1167. }
  1168. if (Style.Language == FormatStyle::LK_Cpp) {
  1169. if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
  1170. Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
  1171. tok::pp_define) &&
  1172. std::find(ForEachMacros.begin(), ForEachMacros.end(),
  1173. FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
  1174. FormatTok->Type = TT_ForEachMacro;
  1175. } else if (FormatTok->is(tok::identifier)) {
  1176. if (MacroBlockBeginRegex.match(Text)) {
  1177. FormatTok->Type = TT_MacroBlockBegin;
  1178. } else if (MacroBlockEndRegex.match(Text)) {
  1179. FormatTok->Type = TT_MacroBlockEnd;
  1180. }
  1181. }
  1182. }
  1183. return FormatTok;
  1184. }
  1185. FormatToken *FormatTok;
  1186. bool IsFirstToken;
  1187. bool GreaterStashed, LessStashed;
  1188. unsigned Column;
  1189. unsigned TrailingWhitespace;
  1190. std::unique_ptr<Lexer> Lex;
  1191. SourceManager &SourceMgr;
  1192. FileID ID;
  1193. FormatStyle &Style;
  1194. IdentifierTable IdentTable;
  1195. AdditionalKeywords Keywords;
  1196. encoding::Encoding Encoding;
  1197. llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
  1198. // Index (in 'Tokens') of the last token that starts a new line.
  1199. unsigned FirstInLineIndex;
  1200. SmallVector<FormatToken *, 16> Tokens;
  1201. SmallVector<IdentifierInfo *, 8> ForEachMacros;
  1202. bool FormattingDisabled;
  1203. llvm::Regex MacroBlockBeginRegex;
  1204. llvm::Regex MacroBlockEndRegex;
  1205. void readRawToken(FormatToken &Tok) {
  1206. Lex->LexFromRawLexer(Tok.Tok);
  1207. Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
  1208. Tok.Tok.getLength());
  1209. // For formatting, treat unterminated string literals like normal string
  1210. // literals.
  1211. if (Tok.is(tok::unknown)) {
  1212. if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
  1213. Tok.Tok.setKind(tok::string_literal);
  1214. Tok.IsUnterminatedLiteral = true;
  1215. } else if (Style.Language == FormatStyle::LK_JavaScript &&
  1216. Tok.TokenText == "''") {
  1217. Tok.Tok.setKind(tok::char_constant);
  1218. }
  1219. }
  1220. if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
  1221. Tok.TokenText == "/* clang-format on */")) {
  1222. FormattingDisabled = false;
  1223. }
  1224. Tok.Finalized = FormattingDisabled;
  1225. if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
  1226. Tok.TokenText == "/* clang-format off */")) {
  1227. FormattingDisabled = true;
  1228. }
  1229. }
  1230. void resetLexer(unsigned Offset) {
  1231. StringRef Buffer = SourceMgr.getBufferData(ID);
  1232. Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
  1233. getFormattingLangOpts(Style), Buffer.begin(),
  1234. Buffer.begin() + Offset, Buffer.end()));
  1235. Lex->SetKeepWhitespaceMode(true);
  1236. TrailingWhitespace = 0;
  1237. }
  1238. };
  1239. static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
  1240. switch (Language) {
  1241. case FormatStyle::LK_Cpp:
  1242. return "C++";
  1243. case FormatStyle::LK_Java:
  1244. return "Java";
  1245. case FormatStyle::LK_JavaScript:
  1246. return "JavaScript";
  1247. case FormatStyle::LK_Proto:
  1248. return "Proto";
  1249. default:
  1250. return "Unknown";
  1251. }
  1252. }
  1253. class Formatter : public UnwrappedLineConsumer {
  1254. public:
  1255. Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID,
  1256. ArrayRef<CharSourceRange> Ranges)
  1257. : Style(Style), ID(ID), SourceMgr(SourceMgr),
  1258. Whitespaces(SourceMgr, Style,
  1259. inputUsesCRLF(SourceMgr.getBufferData(ID))),
  1260. Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
  1261. Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) {
  1262. DEBUG(llvm::dbgs() << "File encoding: "
  1263. << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
  1264. : "unknown")
  1265. << "\n");
  1266. DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
  1267. << "\n");
  1268. }
  1269. tooling::Replacements format(bool *IncompleteFormat) {
  1270. tooling::Replacements Result;
  1271. FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding);
  1272. UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(),
  1273. *this);
  1274. Parser.parse();
  1275. assert(UnwrappedLines.rbegin()->empty());
  1276. for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
  1277. ++Run) {
  1278. DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
  1279. SmallVector<AnnotatedLine *, 16> AnnotatedLines;
  1280. for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
  1281. AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
  1282. }
  1283. tooling::Replacements RunResult =
  1284. format(AnnotatedLines, Tokens, IncompleteFormat);
  1285. DEBUG({
  1286. llvm::dbgs() << "Replacements for run " << Run << ":\n";
  1287. for (tooling::Replacements::iterator I = RunResult.begin(),
  1288. E = RunResult.end();
  1289. I != E; ++I) {
  1290. llvm::dbgs() << I->toString() << "\n";
  1291. }
  1292. });
  1293. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1294. delete AnnotatedLines[i];
  1295. }
  1296. Result.insert(RunResult.begin(), RunResult.end());
  1297. Whitespaces.reset();
  1298. }
  1299. return Result;
  1300. }
  1301. tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  1302. FormatTokenLexer &Tokens,
  1303. bool *IncompleteFormat) {
  1304. TokenAnnotator Annotator(Style, Tokens.getKeywords());
  1305. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1306. Annotator.annotate(*AnnotatedLines[i]);
  1307. }
  1308. deriveLocalStyle(AnnotatedLines);
  1309. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1310. Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
  1311. }
  1312. computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
  1313. Annotator.setCommentLineLevels(AnnotatedLines);
  1314. ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr,
  1315. Whitespaces, Encoding,
  1316. BinPackInconclusiveFunctions);
  1317. UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(),
  1318. IncompleteFormat)
  1319. .format(AnnotatedLines);
  1320. return Whitespaces.generateReplacements();
  1321. }
  1322. private:
  1323. // Determines which lines are affected by the SourceRanges given as input.
  1324. // Returns \c true if at least one line between I and E or one of their
  1325. // children is affected.
  1326. bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
  1327. SmallVectorImpl<AnnotatedLine *>::iterator E) {
  1328. bool SomeLineAffected = false;
  1329. const AnnotatedLine *PreviousLine = nullptr;
  1330. while (I != E) {
  1331. AnnotatedLine *Line = *I;
  1332. Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
  1333. // If a line is part of a preprocessor directive, it needs to be formatted
  1334. // if any token within the directive is affected.
  1335. if (Line->InPPDirective) {
  1336. FormatToken *Last = Line->Last;
  1337. SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
  1338. while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
  1339. Last = (*PPEnd)->Last;
  1340. ++PPEnd;
  1341. }
  1342. if (affectsTokenRange(*Line->First, *Last,
  1343. /*IncludeLeadingNewlines=*/false)) {
  1344. SomeLineAffected = true;
  1345. markAllAsAffected(I, PPEnd);
  1346. }
  1347. I = PPEnd;
  1348. continue;
  1349. }
  1350. if (nonPPLineAffected(Line, PreviousLine))
  1351. SomeLineAffected = true;
  1352. PreviousLine = Line;
  1353. ++I;
  1354. }
  1355. return SomeLineAffected;
  1356. }
  1357. // Determines whether 'Line' is affected by the SourceRanges given as input.
  1358. // Returns \c true if line or one if its children is affected.
  1359. bool nonPPLineAffected(AnnotatedLine *Line,
  1360. const AnnotatedLine *PreviousLine) {
  1361. bool SomeLineAffected = false;
  1362. Line->ChildrenAffected =
  1363. computeAffectedLines(Line->Children.begin(), Line->Children.end());
  1364. if (Line->ChildrenAffected)
  1365. SomeLineAffected = true;
  1366. // Stores whether one of the line's tokens is directly affected.
  1367. bool SomeTokenAffected = false;
  1368. // Stores whether we need to look at the leading newlines of the next token
  1369. // in order to determine whether it was affected.
  1370. bool IncludeLeadingNewlines = false;
  1371. // Stores whether the first child line of any of this line's tokens is
  1372. // affected.
  1373. bool SomeFirstChildAffected = false;
  1374. for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
  1375. // Determine whether 'Tok' was affected.
  1376. if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
  1377. SomeTokenAffected = true;
  1378. // Determine whether the first child of 'Tok' was affected.
  1379. if (!Tok->Children.empty() && Tok->Children.front()->Affected)
  1380. SomeFirstChildAffected = true;
  1381. IncludeLeadingNewlines = Tok->Children.empty();
  1382. }
  1383. // Was this line moved, i.e. has it previously been on the same line as an
  1384. // affected line?
  1385. bool LineMoved = PreviousLine && PreviousLine->Affected &&
  1386. Line->First->NewlinesBefore == 0;
  1387. bool IsContinuedComment =
  1388. Line->First->is(tok::comment) && Line->First->Next == nullptr &&
  1389. Line->First->NewlinesBefore < 2 && PreviousLine &&
  1390. PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
  1391. if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
  1392. IsContinuedComment) {
  1393. Line->Affected = true;
  1394. SomeLineAffected = true;
  1395. }
  1396. return SomeLineAffected;
  1397. }
  1398. // Marks all lines between I and E as well as all their children as affected.
  1399. void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
  1400. SmallVectorImpl<AnnotatedLine *>::iterator E) {
  1401. while (I != E) {
  1402. (*I)->Affected = true;
  1403. markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
  1404. ++I;
  1405. }
  1406. }
  1407. // Returns true if the range from 'First' to 'Last' intersects with one of the
  1408. // input ranges.
  1409. bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
  1410. bool IncludeLeadingNewlines) {
  1411. SourceLocation Start = First.WhitespaceRange.getBegin();
  1412. if (!IncludeLeadingNewlines)
  1413. Start = Start.getLocWithOffset(First.LastNewlineOffset);
  1414. SourceLocation End = Last.getStartOfNonWhitespace();
  1415. End = End.getLocWithOffset(Last.TokenText.size());
  1416. CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
  1417. return affectsCharSourceRange(Range);
  1418. }
  1419. // Returns true if one of the input ranges intersect the leading empty lines
  1420. // before 'Tok'.
  1421. bool affectsLeadingEmptyLines(const FormatToken &Tok) {
  1422. CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
  1423. Tok.WhitespaceRange.getBegin(),
  1424. Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
  1425. return affectsCharSourceRange(EmptyLineRange);
  1426. }
  1427. // Returns true if 'Range' intersects with one of the input ranges.
  1428. bool affectsCharSourceRange(const CharSourceRange &Range) {
  1429. for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
  1430. E = Ranges.end();
  1431. I != E; ++I) {
  1432. if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
  1433. !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
  1434. return true;
  1435. }
  1436. return false;
  1437. }
  1438. static bool inputUsesCRLF(StringRef Text) {
  1439. return Text.count('\r') * 2 > Text.count('\n');
  1440. }
  1441. bool
  1442. hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  1443. for (const AnnotatedLine* Line : Lines) {
  1444. if (hasCpp03IncompatibleFormat(Line->Children))
  1445. return true;
  1446. for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
  1447. if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
  1448. if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
  1449. return true;
  1450. if (Tok->is(TT_TemplateCloser) &&
  1451. Tok->Previous->is(TT_TemplateCloser))
  1452. return true;
  1453. }
  1454. }
  1455. }
  1456. return false;
  1457. }
  1458. int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
  1459. int AlignmentDiff = 0;
  1460. for (const AnnotatedLine* Line : Lines) {
  1461. AlignmentDiff += countVariableAlignments(Line->Children);
  1462. for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
  1463. if (!Tok->is(TT_PointerOrReference))
  1464. continue;
  1465. bool SpaceBefore =
  1466. Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
  1467. bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
  1468. Tok->Next->WhitespaceRange.getEnd();
  1469. if (SpaceBefore && !SpaceAfter)
  1470. ++AlignmentDiff;
  1471. if (!SpaceBefore && SpaceAfter)
  1472. --AlignmentDiff;
  1473. }
  1474. }
  1475. return AlignmentDiff;
  1476. }
  1477. void
  1478. deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  1479. bool HasBinPackedFunction = false;
  1480. bool HasOnePerLineFunction = false;
  1481. for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
  1482. if (!AnnotatedLines[i]->First->Next)
  1483. continue;
  1484. FormatToken *Tok = AnnotatedLines[i]->First->Next;
  1485. while (Tok->Next) {
  1486. if (Tok->PackingKind == PPK_BinPacked)
  1487. HasBinPackedFunction = true;
  1488. if (Tok->PackingKind == PPK_OnePerLine)
  1489. HasOnePerLineFunction = true;
  1490. Tok = Tok->Next;
  1491. }
  1492. }
  1493. if (Style.DerivePointerAlignment)
  1494. Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
  1495. ? FormatStyle::PAS_Left
  1496. : FormatStyle::PAS_Right;
  1497. if (Style.Standard == FormatStyle::LS_Auto)
  1498. Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
  1499. ? FormatStyle::LS_Cpp11
  1500. : FormatStyle::LS_Cpp03;
  1501. BinPackInconclusiveFunctions =
  1502. HasBinPackedFunction || !HasOnePerLineFunction;
  1503. }
  1504. void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
  1505. assert(!UnwrappedLines.empty());
  1506. UnwrappedLines.back().push_back(TheLine);
  1507. }
  1508. void finishRun() override {
  1509. UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
  1510. }
  1511. FormatStyle Style;
  1512. FileID ID;
  1513. SourceManager &SourceMgr;
  1514. WhitespaceManager Whitespaces;
  1515. SmallVector<CharSourceRange, 8> Ranges;
  1516. SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
  1517. encoding::Encoding Encoding;
  1518. bool BinPackInconclusiveFunctions;
  1519. };
  1520. struct IncludeDirective {
  1521. StringRef Filename;
  1522. StringRef Text;
  1523. unsigned Offset;
  1524. unsigned Category;
  1525. };
  1526. } // end anonymous namespace
  1527. // Determines whether 'Ranges' intersects with ('Start', 'End').
  1528. static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
  1529. unsigned End) {
  1530. for (auto Range : Ranges) {
  1531. if (Range.getOffset() < End &&
  1532. Range.getOffset() + Range.getLength() > Start)
  1533. return true;
  1534. }
  1535. return false;
  1536. }
  1537. // Sorts a block of includes given by 'Includes' alphabetically adding the
  1538. // necessary replacement to 'Replaces'. 'Includes' must be in strict source
  1539. // order.
  1540. static void sortIncludes(const FormatStyle &Style,
  1541. const SmallVectorImpl<IncludeDirective> &Includes,
  1542. ArrayRef<tooling::Range> Ranges, StringRef FileName,
  1543. tooling::Replacements &Replaces) {
  1544. if (!affectsRange(Ranges, Includes.front().Offset,
  1545. Includes.back().Offset + Includes.back().Text.size()))
  1546. return;
  1547. SmallVector<unsigned, 16> Indices;
  1548. for (unsigned i = 0, e = Includes.size(); i != e; ++i)
  1549. Indices.push_back(i);
  1550. std::sort(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
  1551. return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
  1552. std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
  1553. });
  1554. // If the #includes are out of order, we generate a single replacement fixing
  1555. // the entire block. Otherwise, no replacement is generated.
  1556. bool OutOfOrder = false;
  1557. for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
  1558. if (Indices[i] != i) {
  1559. OutOfOrder = true;
  1560. break;
  1561. }
  1562. }
  1563. if (!OutOfOrder)
  1564. return;
  1565. std::string result = Includes[Indices[0]].Text;
  1566. for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
  1567. result += "\n";
  1568. result += Includes[Indices[i]].Text;
  1569. }
  1570. // Sorting #includes shouldn't change their total number of characters.
  1571. // This would otherwise mess up 'Ranges'.
  1572. assert(result.size() ==
  1573. Includes.back().Offset + Includes.back().Text.size() -
  1574. Includes.front().Offset);
  1575. Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
  1576. result.size(), result));
  1577. }
  1578. tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
  1579. ArrayRef<tooling::Range> Ranges,
  1580. StringRef FileName) {
  1581. tooling::Replacements Replaces;
  1582. if (!Style.SortIncludes)
  1583. return Replaces;
  1584. unsigned Prev = 0;
  1585. unsigned SearchFrom = 0;
  1586. llvm::Regex IncludeRegex(
  1587. R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))");
  1588. SmallVector<StringRef, 4> Matches;
  1589. SmallVector<IncludeDirective, 16> IncludesInBlock;
  1590. // In compiled files, consider the first #include to be the main #include of
  1591. // the file if it is not a system #include. This ensures that the header
  1592. // doesn't have hidden dependencies
  1593. // (http://llvm.org/docs/CodingStandards.html#include-style).
  1594. //
  1595. // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
  1596. // cases where the first #include is unlikely to be the main header.
  1597. bool LookForMainHeader = FileName.endswith(".c") ||
  1598. FileName.endswith(".cc") ||
  1599. FileName.endswith(".cpp")||
  1600. FileName.endswith(".c++")||
  1601. FileName.endswith(".cxx") ||
  1602. FileName.endswith(".m")||
  1603. FileName.endswith(".mm");
  1604. // Create pre-compiled regular expressions for the #include categories.
  1605. SmallVector<llvm::Regex, 4> CategoryRegexs;
  1606. for (const auto &Category : Style.IncludeCategories)
  1607. CategoryRegexs.emplace_back(Category.Regex);
  1608. for (;;) {
  1609. auto Pos = Code.find('\n', SearchFrom);
  1610. StringRef Line =
  1611. Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
  1612. if (!Line.endswith("\\")) {
  1613. if (IncludeRegex.match(Line, &Matches)) {
  1614. StringRef IncludeName = Matches[2];
  1615. unsigned Category;
  1616. if (LookForMainHeader && !IncludeName.startswith("<")) {
  1617. Category = 0;
  1618. } else {
  1619. Category = UINT_MAX;
  1620. for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) {
  1621. if (CategoryRegexs[i].match(IncludeName)) {
  1622. Category = Style.IncludeCategories[i].Priority;
  1623. break;
  1624. }
  1625. }
  1626. }
  1627. LookForMainHeader = false;
  1628. IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
  1629. } else if (!IncludesInBlock.empty()) {
  1630. sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
  1631. IncludesInBlock.clear();
  1632. }
  1633. Prev = Pos + 1;
  1634. }
  1635. if (Pos == StringRef::npos || Pos + 1 == Code.size())
  1636. break;
  1637. SearchFrom = Pos + 1;
  1638. }
  1639. if (!IncludesInBlock.empty())
  1640. sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
  1641. return Replaces;
  1642. }
  1643. tooling::Replacements reformat(const FormatStyle &Style,
  1644. SourceManager &SourceMgr, FileID ID,
  1645. ArrayRef<CharSourceRange> Ranges,
  1646. bool *IncompleteFormat) {
  1647. FormatStyle Expanded = expandPresets(Style);
  1648. if (Expanded.DisableFormat)
  1649. return tooling::Replacements();
  1650. Formatter formatter(Expanded, SourceMgr, ID, Ranges);
  1651. return formatter.format(IncompleteFormat);
  1652. }
  1653. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
  1654. ArrayRef<tooling::Range> Ranges,
  1655. StringRef FileName, bool *IncompleteFormat) {
  1656. if (Style.DisableFormat)
  1657. return tooling::Replacements();
  1658. IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
  1659. new vfs::InMemoryFileSystem);
  1660. FileManager Files(FileSystemOptions(), InMemoryFileSystem);
  1661. DiagnosticsEngine Diagnostics(
  1662. IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
  1663. new DiagnosticOptions);
  1664. SourceManager SourceMgr(Diagnostics, Files);
  1665. InMemoryFileSystem->addFile(FileName, 0,
  1666. llvm::MemoryBuffer::getMemBuffer(Code, FileName));
  1667. FileID ID = SourceMgr.createFileID(Files.getFile(FileName), SourceLocation(),
  1668. clang::SrcMgr::C_User);
  1669. SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
  1670. std::vector<CharSourceRange> CharRanges;
  1671. for (const tooling::Range &Range : Ranges) {
  1672. SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
  1673. SourceLocation End = Start.getLocWithOffset(Range.getLength());
  1674. CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
  1675. }
  1676. return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat);
  1677. }
  1678. LangOptions getFormattingLangOpts(const FormatStyle &Style) {
  1679. LangOptions LangOpts;
  1680. LangOpts.CPlusPlus = 1;
  1681. LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1682. LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
  1683. LangOpts.LineComment = 1;
  1684. bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
  1685. LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
  1686. LangOpts.Bool = 1;
  1687. LangOpts.ObjC1 = 1;
  1688. LangOpts.ObjC2 = 1;
  1689. LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
  1690. LangOpts.DeclSpecKeyword = 1; // To get __declspec.
  1691. return LangOpts;
  1692. }
  1693. const char *StyleOptionHelpDescription =
  1694. "Coding style, currently supports:\n"
  1695. " LLVM, Google, Chromium, Mozilla, WebKit.\n"
  1696. "Use -style=file to load style configuration from\n"
  1697. ".clang-format file located in one of the parent\n"
  1698. "directories of the source file (or current\n"
  1699. "directory for stdin).\n"
  1700. "Use -style=\"{key: value, ...}\" to set specific\n"
  1701. "parameters, e.g.:\n"
  1702. " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
  1703. static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
  1704. if (FileName.endswith(".java")) {
  1705. return FormatStyle::LK_Java;
  1706. } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) {
  1707. // JavaScript or TypeScript.
  1708. return FormatStyle::LK_JavaScript;
  1709. } else if (FileName.endswith_lower(".proto") ||
  1710. FileName.endswith_lower(".protodevel")) {
  1711. return FormatStyle::LK_Proto;
  1712. }
  1713. return FormatStyle::LK_Cpp;
  1714. }
  1715. FormatStyle getStyle(StringRef StyleName, StringRef FileName,
  1716. StringRef FallbackStyle) {
  1717. FormatStyle Style = getLLVMStyle();
  1718. Style.Language = getLanguageByFileName(FileName);
  1719. if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
  1720. llvm::errs() << "Invalid fallback style \"" << FallbackStyle
  1721. << "\" using LLVM style\n";
  1722. return Style;
  1723. }
  1724. if (StyleName.startswith("{")) {
  1725. // Parse YAML/JSON style from the command line.
  1726. if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
  1727. llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
  1728. << FallbackStyle << " style\n";
  1729. }
  1730. return Style;
  1731. }
  1732. if (!StyleName.equals_lower("file")) {
  1733. if (!getPredefinedStyle(StyleName, Style.Language, &Style))
  1734. llvm::errs() << "Invalid value for -style, using " << FallbackStyle
  1735. << " style\n";
  1736. return Style;
  1737. }
  1738. // Look for .clang-format/_clang-format file in the file's parent directories.
  1739. SmallString<128> UnsuitableConfigFiles;
  1740. SmallString<128> Path(FileName);
  1741. llvm::sys::fs::make_absolute(Path);
  1742. for (StringRef Directory = Path; !Directory.empty();
  1743. Directory = llvm::sys::path::parent_path(Directory)) {
  1744. if (!llvm::sys::fs::is_directory(Directory))
  1745. continue;
  1746. SmallString<128> ConfigFile(Directory);
  1747. llvm::sys::path::append(ConfigFile, ".clang-format");
  1748. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1749. bool IsFile = false;
  1750. // Ignore errors from is_regular_file: we only need to know if we can read
  1751. // the file or not.
  1752. llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
  1753. if (!IsFile) {
  1754. // Try _clang-format too, since dotfiles are not commonly used on Windows.
  1755. ConfigFile = Directory;
  1756. llvm::sys::path::append(ConfigFile, "_clang-format");
  1757. DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
  1758. llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
  1759. }
  1760. if (IsFile) {
  1761. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
  1762. llvm::MemoryBuffer::getFile(ConfigFile.c_str());
  1763. if (std::error_code EC = Text.getError()) {
  1764. llvm::errs() << EC.message() << "\n";
  1765. break;
  1766. }
  1767. if (std::error_code ec =
  1768. parseConfiguration(Text.get()->getBuffer(), &Style)) {
  1769. if (ec == ParseError::Unsuitable) {
  1770. if (!UnsuitableConfigFiles.empty())
  1771. UnsuitableConfigFiles.append(", ");
  1772. UnsuitableConfigFiles.append(ConfigFile);
  1773. continue;
  1774. }
  1775. llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
  1776. << "\n";
  1777. break;
  1778. }
  1779. DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
  1780. return Style;
  1781. }
  1782. }
  1783. if (!UnsuitableConfigFiles.empty()) {
  1784. llvm::errs() << "Configuration file(s) do(es) not support "
  1785. << getLanguageName(Style.Language) << ": "
  1786. << UnsuitableConfigFiles << "\n";
  1787. }
  1788. return Style;
  1789. }
  1790. } // namespace format
  1791. } // namespace clang