DependencyDirectivesSourceMinimizer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. //===- DependencyDirectivesSourceMinimizer.cpp - -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This is the implementation for minimizing header and source files to the
  11. /// minimum necessary preprocessor directives for evaluating includes. It
  12. /// reduces the source down to #define, #include, #import, @import, and any
  13. /// conditional preprocessor logic that contains one of those.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
  17. #include "clang/Basic/CharInfo.h"
  18. #include "clang/Basic/Diagnostic.h"
  19. #include "clang/Lex/LexDiagnostic.h"
  20. #include "llvm/ADT/StringSwitch.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. using namespace llvm;
  23. using namespace clang;
  24. using namespace clang::minimize_source_to_dependency_directives;
  25. namespace {
  26. struct Minimizer {
  27. /// Minimized output.
  28. SmallVectorImpl<char> &Out;
  29. /// The known tokens encountered during the minimization.
  30. SmallVectorImpl<Token> &Tokens;
  31. Minimizer(SmallVectorImpl<char> &Out, SmallVectorImpl<Token> &Tokens,
  32. StringRef Input, DiagnosticsEngine *Diags,
  33. SourceLocation InputSourceLoc)
  34. : Out(Out), Tokens(Tokens), Input(Input), Diags(Diags),
  35. InputSourceLoc(InputSourceLoc) {}
  36. /// Lex the provided source and emit the minimized output.
  37. ///
  38. /// \returns True on error.
  39. bool minimize();
  40. private:
  41. struct IdInfo {
  42. const char *Last;
  43. StringRef Name;
  44. };
  45. /// Lex an identifier.
  46. ///
  47. /// \pre First points at a valid identifier head.
  48. LLVM_NODISCARD IdInfo lexIdentifier(const char *First, const char *const End);
  49. LLVM_NODISCARD bool isNextIdentifier(StringRef Id, const char *&First,
  50. const char *const End);
  51. LLVM_NODISCARD bool minimizeImpl(const char *First, const char *const End);
  52. LLVM_NODISCARD bool lexPPLine(const char *&First, const char *const End);
  53. LLVM_NODISCARD bool lexAt(const char *&First, const char *const End);
  54. LLVM_NODISCARD bool lexDefine(const char *&First, const char *const End);
  55. LLVM_NODISCARD bool lexPragma(const char *&First, const char *const End);
  56. LLVM_NODISCARD bool lexEndif(const char *&First, const char *const End);
  57. LLVM_NODISCARD bool lexDefault(TokenKind Kind, StringRef Directive,
  58. const char *&First, const char *const End);
  59. Token &makeToken(TokenKind K) {
  60. Tokens.emplace_back(K, Out.size());
  61. return Tokens.back();
  62. }
  63. void popToken() {
  64. Out.resize(Tokens.back().Offset);
  65. Tokens.pop_back();
  66. }
  67. TokenKind top() const { return Tokens.empty() ? pp_none : Tokens.back().K; }
  68. Minimizer &put(char Byte) {
  69. Out.push_back(Byte);
  70. return *this;
  71. }
  72. Minimizer &append(StringRef S) { return append(S.begin(), S.end()); }
  73. Minimizer &append(const char *First, const char *Last) {
  74. Out.append(First, Last);
  75. return *this;
  76. }
  77. void printToNewline(const char *&First, const char *const End);
  78. void printAdjacentModuleNameParts(const char *&First, const char *const End);
  79. LLVM_NODISCARD bool printAtImportBody(const char *&First,
  80. const char *const End);
  81. void printDirectiveBody(const char *&First, const char *const End);
  82. void printAdjacentMacroArgs(const char *&First, const char *const End);
  83. LLVM_NODISCARD bool printMacroArgs(const char *&First, const char *const End);
  84. /// Reports a diagnostic if the diagnostic engine is provided. Always returns
  85. /// true at the end.
  86. bool reportError(const char *CurPtr, unsigned Err);
  87. StringMap<char> SplitIds;
  88. StringRef Input;
  89. DiagnosticsEngine *Diags;
  90. SourceLocation InputSourceLoc;
  91. };
  92. } // end anonymous namespace
  93. bool Minimizer::reportError(const char *CurPtr, unsigned Err) {
  94. if (!Diags)
  95. return true;
  96. assert(CurPtr >= Input.data() && "invalid buffer ptr");
  97. Diags->Report(InputSourceLoc.getLocWithOffset(CurPtr - Input.data()), Err);
  98. return true;
  99. }
  100. static void skipOverSpaces(const char *&First, const char *const End) {
  101. while (First != End && isHorizontalWhitespace(*First))
  102. ++First;
  103. }
  104. LLVM_NODISCARD static bool isRawStringLiteral(const char *First,
  105. const char *Current) {
  106. assert(First <= Current);
  107. // Check if we can even back up.
  108. if (*Current != '\"' || First == Current)
  109. return false;
  110. // Check for an "R".
  111. --Current;
  112. if (*Current != 'R')
  113. return false;
  114. if (First == Current || !isIdentifierBody(*--Current))
  115. return true;
  116. // Check for a prefix of "u", "U", or "L".
  117. if (*Current == 'u' || *Current == 'U' || *Current == 'L')
  118. return First == Current || !isIdentifierBody(*--Current);
  119. // Check for a prefix of "u8".
  120. if (*Current != '8' || First == Current || *Current-- != 'u')
  121. return false;
  122. return First == Current || !isIdentifierBody(*--Current);
  123. }
  124. static void skipRawString(const char *&First, const char *const End) {
  125. assert(First[0] == '\"');
  126. assert(First[-1] == 'R');
  127. const char *Last = ++First;
  128. while (Last != End && *Last != '(')
  129. ++Last;
  130. if (Last == End) {
  131. First = Last; // Hit the end... just give up.
  132. return;
  133. }
  134. StringRef Terminator(First, Last - First);
  135. for (;;) {
  136. // Move First to just past the next ")".
  137. First = Last;
  138. while (First != End && *First != ')')
  139. ++First;
  140. if (First == End)
  141. return;
  142. ++First;
  143. // Look ahead for the terminator sequence.
  144. Last = First;
  145. while (Last != End && size_t(Last - First) < Terminator.size() &&
  146. Terminator[Last - First] == *Last)
  147. ++Last;
  148. // Check if we hit it (or the end of the file).
  149. if (Last == End) {
  150. First = Last;
  151. return;
  152. }
  153. if (size_t(Last - First) < Terminator.size())
  154. continue;
  155. if (*Last != '\"')
  156. continue;
  157. First = Last + 1;
  158. return;
  159. }
  160. }
  161. static void skipString(const char *&First, const char *const End) {
  162. assert(*First == '\'' || *First == '\"');
  163. const char Terminator = *First;
  164. for (++First; First != End && *First != Terminator; ++First)
  165. if (*First == '\\')
  166. if (++First == End)
  167. return;
  168. if (First != End)
  169. ++First; // Finish off the string.
  170. }
  171. static void skipNewline(const char *&First, const char *End) {
  172. assert(isVerticalWhitespace(*First));
  173. ++First;
  174. if (First == End)
  175. return;
  176. // Check for "\n\r" and "\r\n".
  177. if (LLVM_UNLIKELY(isVerticalWhitespace(*First) && First[-1] != First[0]))
  178. ++First;
  179. }
  180. static void skipToNewlineRaw(const char *&First, const char *const End) {
  181. for (;;) {
  182. if (First == End)
  183. return;
  184. if (isVerticalWhitespace(*First))
  185. return;
  186. while (!isVerticalWhitespace(*First))
  187. if (++First == End)
  188. return;
  189. if (First[-1] != '\\')
  190. return;
  191. ++First; // Keep going...
  192. }
  193. }
  194. static const char *reverseOverSpaces(const char *First, const char *Last) {
  195. assert(First <= Last);
  196. while (First != Last && isHorizontalWhitespace(Last[-1]))
  197. --Last;
  198. return Last;
  199. }
  200. static void skipLineComment(const char *&First, const char *const End) {
  201. assert(First[0] == '/' && First[1] == '/');
  202. First += 2;
  203. skipToNewlineRaw(First, End);
  204. }
  205. static void skipBlockComment(const char *&First, const char *const End) {
  206. assert(First[0] == '/' && First[1] == '*');
  207. if (End - First < 4) {
  208. First = End;
  209. return;
  210. }
  211. for (First += 3; First != End; ++First)
  212. if (First[-1] == '*' && First[0] == '/') {
  213. ++First;
  214. return;
  215. }
  216. }
  217. /// \returns True if the current single quotation mark character is a C++ 14
  218. /// digit separator.
  219. static bool isQuoteCppDigitSeparator(const char *const Start,
  220. const char *const Cur,
  221. const char *const End) {
  222. assert(*Cur == '\'' && "expected quotation character");
  223. // skipLine called in places where we don't expect a valid number
  224. // body before `start` on the same line, so always return false at the start.
  225. if (Start == Cur)
  226. return false;
  227. // The previous character must be a valid PP number character.
  228. if (!isPreprocessingNumberBody(*(Cur - 1)))
  229. return false;
  230. // The next character should be a valid identifier body character.
  231. return (Cur + 1) < End && isIdentifierBody(*(Cur + 1));
  232. }
  233. static void skipLine(const char *&First, const char *const End) {
  234. do {
  235. assert(First <= End);
  236. if (First == End)
  237. return;
  238. if (isVerticalWhitespace(*First)) {
  239. skipNewline(First, End);
  240. return;
  241. }
  242. const char *Start = First;
  243. while (First != End && !isVerticalWhitespace(*First)) {
  244. // Iterate over strings correctly to avoid comments and newlines.
  245. if (*First == '\"' ||
  246. (*First == '\'' && !isQuoteCppDigitSeparator(Start, First, End))) {
  247. if (isRawStringLiteral(Start, First))
  248. skipRawString(First, End);
  249. else
  250. skipString(First, End);
  251. continue;
  252. }
  253. // Iterate over comments correctly.
  254. if (*First != '/' || End - First < 2) {
  255. ++First;
  256. continue;
  257. }
  258. if (First[1] == '/') {
  259. // "//...".
  260. skipLineComment(First, End);
  261. continue;
  262. }
  263. if (First[1] != '*') {
  264. ++First;
  265. continue;
  266. }
  267. // "/*...*/".
  268. skipBlockComment(First, End);
  269. }
  270. if (First == End)
  271. return;
  272. // Skip over the newline.
  273. assert(isVerticalWhitespace(*First));
  274. skipNewline(First, End);
  275. } while (First[-2] == '\\'); // Continue past line-continuations.
  276. }
  277. static void skipDirective(StringRef Name, const char *&First,
  278. const char *const End) {
  279. if (llvm::StringSwitch<bool>(Name)
  280. .Case("warning", true)
  281. .Case("error", true)
  282. .Default(false))
  283. // Do not process quotes or comments.
  284. skipToNewlineRaw(First, End);
  285. else
  286. skipLine(First, End);
  287. }
  288. void Minimizer::printToNewline(const char *&First, const char *const End) {
  289. while (First != End && !isVerticalWhitespace(*First)) {
  290. const char *Last = First;
  291. do {
  292. // Iterate over strings correctly to avoid comments and newlines.
  293. if (*Last == '\"' || *Last == '\'') {
  294. if (LLVM_UNLIKELY(isRawStringLiteral(First, Last)))
  295. skipRawString(Last, End);
  296. else
  297. skipString(Last, End);
  298. continue;
  299. }
  300. if (*Last != '/' || End - Last < 2) {
  301. ++Last;
  302. continue; // Gather the rest up to print verbatim.
  303. }
  304. if (Last[1] != '/' && Last[1] != '*') {
  305. ++Last;
  306. continue;
  307. }
  308. // Deal with "//..." and "/*...*/".
  309. append(First, reverseOverSpaces(First, Last));
  310. First = Last;
  311. if (Last[1] == '/') {
  312. skipLineComment(First, End);
  313. return;
  314. }
  315. put(' ');
  316. skipBlockComment(First, End);
  317. skipOverSpaces(First, End);
  318. Last = First;
  319. } while (Last != End && !isVerticalWhitespace(*Last));
  320. // Print out the string.
  321. if (Last == End || Last == First || Last[-1] != '\\') {
  322. append(First, reverseOverSpaces(First, Last));
  323. return;
  324. }
  325. // Print up to the backslash, backing up over spaces.
  326. append(First, reverseOverSpaces(First, Last - 1));
  327. First = Last;
  328. skipNewline(First, End);
  329. skipOverSpaces(First, End);
  330. }
  331. }
  332. static void skipWhitespace(const char *&First, const char *const End) {
  333. for (;;) {
  334. assert(First <= End);
  335. skipOverSpaces(First, End);
  336. if (End - First < 2)
  337. return;
  338. if (First[0] == '\\' && isVerticalWhitespace(First[1])) {
  339. skipNewline(++First, End);
  340. continue;
  341. }
  342. // Check for a non-comment character.
  343. if (First[0] != '/')
  344. return;
  345. // "// ...".
  346. if (First[1] == '/') {
  347. skipLineComment(First, End);
  348. return;
  349. }
  350. // Cannot be a comment.
  351. if (First[1] != '*')
  352. return;
  353. // "/*...*/".
  354. skipBlockComment(First, End);
  355. }
  356. }
  357. void Minimizer::printAdjacentModuleNameParts(const char *&First,
  358. const char *const End) {
  359. // Skip over parts of the body.
  360. const char *Last = First;
  361. do
  362. ++Last;
  363. while (Last != End && (isIdentifierBody(*Last) || *Last == '.'));
  364. append(First, Last);
  365. First = Last;
  366. }
  367. bool Minimizer::printAtImportBody(const char *&First, const char *const End) {
  368. for (;;) {
  369. skipWhitespace(First, End);
  370. if (First == End)
  371. return true;
  372. if (isVerticalWhitespace(*First)) {
  373. skipNewline(First, End);
  374. continue;
  375. }
  376. // Found a semicolon.
  377. if (*First == ';') {
  378. put(*First++).put('\n');
  379. return false;
  380. }
  381. // Don't handle macro expansions inside @import for now.
  382. if (!isIdentifierBody(*First) && *First != '.')
  383. return true;
  384. printAdjacentModuleNameParts(First, End);
  385. }
  386. }
  387. void Minimizer::printDirectiveBody(const char *&First, const char *const End) {
  388. skipWhitespace(First, End); // Skip initial whitespace.
  389. printToNewline(First, End);
  390. while (Out.back() == ' ')
  391. Out.pop_back();
  392. put('\n');
  393. }
  394. LLVM_NODISCARD static const char *lexRawIdentifier(const char *First,
  395. const char *const End) {
  396. assert(isIdentifierBody(*First) && "invalid identifer");
  397. const char *Last = First + 1;
  398. while (Last != End && isIdentifierBody(*Last))
  399. ++Last;
  400. return Last;
  401. }
  402. LLVM_NODISCARD static const char *
  403. getIdentifierContinuation(const char *First, const char *const End) {
  404. if (End - First < 3 || First[0] != '\\' || !isVerticalWhitespace(First[1]))
  405. return nullptr;
  406. ++First;
  407. skipNewline(First, End);
  408. if (First == End)
  409. return nullptr;
  410. return isIdentifierBody(First[0]) ? First : nullptr;
  411. }
  412. Minimizer::IdInfo Minimizer::lexIdentifier(const char *First,
  413. const char *const End) {
  414. const char *Last = lexRawIdentifier(First, End);
  415. const char *Next = getIdentifierContinuation(Last, End);
  416. if (LLVM_LIKELY(!Next))
  417. return IdInfo{Last, StringRef(First, Last - First)};
  418. // Slow path, where identifiers are split over lines.
  419. SmallVector<char, 64> Id(First, Last);
  420. while (Next) {
  421. Last = lexRawIdentifier(Next, End);
  422. Id.append(Next, Last);
  423. Next = getIdentifierContinuation(Last, End);
  424. }
  425. return IdInfo{
  426. Last,
  427. SplitIds.try_emplace(StringRef(Id.begin(), Id.size()), 0).first->first()};
  428. }
  429. void Minimizer::printAdjacentMacroArgs(const char *&First,
  430. const char *const End) {
  431. // Skip over parts of the body.
  432. const char *Last = First;
  433. do
  434. ++Last;
  435. while (Last != End &&
  436. (isIdentifierBody(*Last) || *Last == '.' || *Last == ','));
  437. append(First, Last);
  438. First = Last;
  439. }
  440. bool Minimizer::printMacroArgs(const char *&First, const char *const End) {
  441. assert(*First == '(');
  442. put(*First++);
  443. for (;;) {
  444. skipWhitespace(First, End);
  445. if (First == End)
  446. return true;
  447. if (*First == ')') {
  448. put(*First++);
  449. return false;
  450. }
  451. // This is intentionally fairly liberal.
  452. if (!(isIdentifierBody(*First) || *First == '.' || *First == ','))
  453. return true;
  454. printAdjacentMacroArgs(First, End);
  455. }
  456. }
  457. /// Looks for an identifier starting from Last.
  458. ///
  459. /// Updates "First" to just past the next identifier, if any. Returns true iff
  460. /// the identifier matches "Id".
  461. bool Minimizer::isNextIdentifier(StringRef Id, const char *&First,
  462. const char *const End) {
  463. skipWhitespace(First, End);
  464. if (First == End || !isIdentifierHead(*First))
  465. return false;
  466. IdInfo FoundId = lexIdentifier(First, End);
  467. First = FoundId.Last;
  468. return FoundId.Name == Id;
  469. }
  470. bool Minimizer::lexAt(const char *&First, const char *const End) {
  471. // Handle "@import".
  472. const char *ImportLoc = First++;
  473. if (!isNextIdentifier("import", First, End)) {
  474. skipLine(First, End);
  475. return false;
  476. }
  477. makeToken(decl_at_import);
  478. append("@import ");
  479. if (printAtImportBody(First, End))
  480. return reportError(
  481. ImportLoc, diag::err_dep_source_minimizer_missing_sema_after_at_import);
  482. skipWhitespace(First, End);
  483. if (First == End)
  484. return false;
  485. if (!isVerticalWhitespace(*First))
  486. return reportError(
  487. ImportLoc, diag::err_dep_source_minimizer_unexpected_tokens_at_import);
  488. skipNewline(First, End);
  489. return false;
  490. }
  491. bool Minimizer::lexDefine(const char *&First, const char *const End) {
  492. makeToken(pp_define);
  493. append("#define ");
  494. skipWhitespace(First, End);
  495. if (!isIdentifierHead(*First))
  496. return reportError(First, diag::err_pp_macro_not_identifier);
  497. IdInfo Id = lexIdentifier(First, End);
  498. const char *Last = Id.Last;
  499. append(Id.Name);
  500. if (Last == End)
  501. return false;
  502. if (*Last == '(') {
  503. size_t Size = Out.size();
  504. if (printMacroArgs(Last, End)) {
  505. // Be robust to bad macro arguments, since they can show up in disabled
  506. // code.
  507. Out.resize(Size);
  508. append("(/* invalid */\n");
  509. skipLine(Last, End);
  510. return false;
  511. }
  512. }
  513. skipWhitespace(Last, End);
  514. if (Last == End)
  515. return false;
  516. if (!isVerticalWhitespace(*Last))
  517. put(' ');
  518. printDirectiveBody(Last, End);
  519. First = Last;
  520. return false;
  521. }
  522. bool Minimizer::lexPragma(const char *&First, const char *const End) {
  523. // #pragma.
  524. if (!isNextIdentifier("clang", First, End)) {
  525. skipLine(First, End);
  526. return false;
  527. }
  528. // #pragma clang.
  529. if (!isNextIdentifier("module", First, End)) {
  530. skipLine(First, End);
  531. return false;
  532. }
  533. // #pragma clang module.
  534. if (!isNextIdentifier("import", First, End)) {
  535. skipLine(First, End);
  536. return false;
  537. }
  538. // #pragma clang module import.
  539. makeToken(pp_pragma_import);
  540. append("#pragma clang module import ");
  541. printDirectiveBody(First, End);
  542. return false;
  543. }
  544. bool Minimizer::lexEndif(const char *&First, const char *const End) {
  545. // Strip out "#else" if it's empty.
  546. if (top() == pp_else)
  547. popToken();
  548. // Strip out "#elif" if they're empty.
  549. while (top() == pp_elif)
  550. popToken();
  551. // If "#if" is empty, strip it and skip the "#endif".
  552. if (top() == pp_if || top() == pp_ifdef || top() == pp_ifndef) {
  553. popToken();
  554. skipLine(First, End);
  555. return false;
  556. }
  557. return lexDefault(pp_endif, "endif", First, End);
  558. }
  559. bool Minimizer::lexDefault(TokenKind Kind, StringRef Directive,
  560. const char *&First, const char *const End) {
  561. makeToken(Kind);
  562. put('#').append(Directive).put(' ');
  563. printDirectiveBody(First, End);
  564. return false;
  565. }
  566. bool Minimizer::lexPPLine(const char *&First, const char *const End) {
  567. assert(First != End);
  568. skipWhitespace(First, End);
  569. assert(First <= End);
  570. if (First == End)
  571. return false;
  572. if (*First != '#' && *First != '@') {
  573. skipLine(First, End);
  574. assert(First <= End);
  575. return false;
  576. }
  577. // Handle "@import".
  578. if (*First == '@')
  579. return lexAt(First, End);
  580. // Handle preprocessing directives.
  581. ++First; // Skip over '#'.
  582. skipWhitespace(First, End);
  583. if (First == End)
  584. return reportError(First, diag::err_pp_expected_eol);
  585. if (!isIdentifierHead(*First)) {
  586. skipLine(First, End);
  587. return false;
  588. }
  589. // Figure out the token.
  590. IdInfo Id = lexIdentifier(First, End);
  591. First = Id.Last;
  592. auto Kind = llvm::StringSwitch<TokenKind>(Id.Name)
  593. .Case("include", pp_include)
  594. .Case("__include_macros", pp___include_macros)
  595. .Case("define", pp_define)
  596. .Case("undef", pp_undef)
  597. .Case("import", pp_import)
  598. .Case("include_next", pp_include_next)
  599. .Case("if", pp_if)
  600. .Case("ifdef", pp_ifdef)
  601. .Case("ifndef", pp_ifndef)
  602. .Case("elif", pp_elif)
  603. .Case("else", pp_else)
  604. .Case("endif", pp_endif)
  605. .Case("pragma", pp_pragma_import)
  606. .Default(pp_none);
  607. if (Kind == pp_none) {
  608. skipDirective(Id.Name, First, End);
  609. return false;
  610. }
  611. if (Kind == pp_endif)
  612. return lexEndif(First, End);
  613. if (Kind == pp_define)
  614. return lexDefine(First, End);
  615. if (Kind == pp_pragma_import)
  616. return lexPragma(First, End);
  617. // Everything else.
  618. return lexDefault(Kind, Id.Name, First, End);
  619. }
  620. bool Minimizer::minimizeImpl(const char *First, const char *const End) {
  621. while (First != End)
  622. if (lexPPLine(First, End))
  623. return true;
  624. return false;
  625. }
  626. bool Minimizer::minimize() {
  627. bool Error = minimizeImpl(Input.begin(), Input.end());
  628. if (!Error) {
  629. // Add a trailing newline and an EOF on success.
  630. if (!Out.empty() && Out.back() != '\n')
  631. Out.push_back('\n');
  632. makeToken(pp_eof);
  633. }
  634. // Null-terminate the output. This way the memory buffer that's passed to
  635. // Clang will not have to worry about the terminating '\0'.
  636. Out.push_back(0);
  637. Out.pop_back();
  638. return Error;
  639. }
  640. bool clang::minimizeSourceToDependencyDirectives(
  641. StringRef Input, SmallVectorImpl<char> &Output,
  642. SmallVectorImpl<Token> &Tokens, DiagnosticsEngine *Diags,
  643. SourceLocation InputSourceLoc) {
  644. Output.clear();
  645. Tokens.clear();
  646. return Minimizer(Output, Tokens, Input, Diags, InputSourceLoc).minimize();
  647. }