Format.cpp 102 KB

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