Format.cpp 84 KB

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