Format.cpp 102 KB

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