Format.cpp 86 KB

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