Format.cpp 65 KB

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