clang  14.0.0git
DependencyDirectivesSourceMinimizer.cpp
Go to the documentation of this file.
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 
18 #include "clang/Basic/CharInfo.h"
19 #include "clang/Basic/Diagnostic.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 
25 using namespace llvm;
26 using namespace clang;
28 
29 namespace {
30 
31 struct Minimizer {
32  /// Minimized output.
34  /// The known tokens encountered during the minimization.
35  SmallVectorImpl<Token> &Tokens;
36 
37  Minimizer(SmallVectorImpl<char> &Out, SmallVectorImpl<Token> &Tokens,
38  StringRef Input, DiagnosticsEngine *Diags,
39  SourceLocation InputSourceLoc)
40  : Out(Out), Tokens(Tokens), Input(Input), Diags(Diags),
41  InputSourceLoc(InputSourceLoc) {}
42 
43  /// Lex the provided source and emit the minimized output.
44  ///
45  /// \returns True on error.
46  bool minimize();
47 
48 private:
49  struct IdInfo {
50  const char *Last;
51  StringRef Name;
52  };
53 
54  /// Lex an identifier.
55  ///
56  /// \pre First points at a valid identifier head.
57  LLVM_NODISCARD IdInfo lexIdentifier(const char *First, const char *const End);
58  LLVM_NODISCARD bool isNextIdentifier(StringRef Id, const char *&First,
59  const char *const End);
60  LLVM_NODISCARD bool minimizeImpl(const char *First, const char *const End);
61  LLVM_NODISCARD bool lexPPLine(const char *&First, const char *const End);
62  LLVM_NODISCARD bool lexAt(const char *&First, const char *const End);
63  LLVM_NODISCARD bool lexModule(const char *&First, const char *const End);
64  LLVM_NODISCARD bool lexDefine(const char *&First, const char *const End);
65  LLVM_NODISCARD bool lexPragma(const char *&First, const char *const End);
66  LLVM_NODISCARD bool lexEndif(const char *&First, const char *const End);
67  LLVM_NODISCARD bool lexDefault(TokenKind Kind, StringRef Directive,
68  const char *&First, const char *const End);
69  Token &makeToken(TokenKind K) {
70  Tokens.emplace_back(K, Out.size());
71  return Tokens.back();
72  }
73  void popToken() {
74  Out.resize(Tokens.back().Offset);
75  Tokens.pop_back();
76  }
77  TokenKind top() const { return Tokens.empty() ? pp_none : Tokens.back().K; }
78 
79  Minimizer &put(char Byte) {
80  Out.push_back(Byte);
81  return *this;
82  }
83  Minimizer &append(StringRef S) { return append(S.begin(), S.end()); }
84  Minimizer &append(const char *First, const char *Last) {
85  Out.append(First, Last);
86  return *this;
87  }
88 
89  void printToNewline(const char *&First, const char *const End);
90  void printAdjacentModuleNameParts(const char *&First, const char *const End);
91  LLVM_NODISCARD bool printAtImportBody(const char *&First,
92  const char *const End);
93  void printDirectiveBody(const char *&First, const char *const End);
94  void printAdjacentMacroArgs(const char *&First, const char *const End);
95  LLVM_NODISCARD bool printMacroArgs(const char *&First, const char *const End);
96 
97  /// Reports a diagnostic if the diagnostic engine is provided. Always returns
98  /// true at the end.
99  bool reportError(const char *CurPtr, unsigned Err);
100 
101  StringMap<char> SplitIds;
102  StringRef Input;
103  DiagnosticsEngine *Diags;
104  SourceLocation InputSourceLoc;
105 };
106 
107 } // end anonymous namespace
108 
109 bool Minimizer::reportError(const char *CurPtr, unsigned Err) {
110  if (!Diags)
111  return true;
112  assert(CurPtr >= Input.data() && "invalid buffer ptr");
113  Diags->Report(InputSourceLoc.getLocWithOffset(CurPtr - Input.data()), Err);
114  return true;
115 }
116 
117 static void skipOverSpaces(const char *&First, const char *const End) {
118  while (First != End && isHorizontalWhitespace(*First))
119  ++First;
120 }
121 
122 LLVM_NODISCARD static bool isRawStringLiteral(const char *First,
123  const char *Current) {
124  assert(First <= Current);
125 
126  // Check if we can even back up.
127  if (*Current != '"' || First == Current)
128  return false;
129 
130  // Check for an "R".
131  --Current;
132  if (*Current != 'R')
133  return false;
134  if (First == Current || !isAsciiIdentifierContinue(*--Current))
135  return true;
136 
137  // Check for a prefix of "u", "U", or "L".
138  if (*Current == 'u' || *Current == 'U' || *Current == 'L')
139  return First == Current || !isAsciiIdentifierContinue(*--Current);
140 
141  // Check for a prefix of "u8".
142  if (*Current != '8' || First == Current || *Current-- != 'u')
143  return false;
144  return First == Current || !isAsciiIdentifierContinue(*--Current);
145 }
146 
147 static void skipRawString(const char *&First, const char *const End) {
148  assert(First[0] == '"');
149  assert(First[-1] == 'R');
150 
151  const char *Last = ++First;
152  while (Last != End && *Last != '(')
153  ++Last;
154  if (Last == End) {
155  First = Last; // Hit the end... just give up.
156  return;
157  }
158 
159  StringRef Terminator(First, Last - First);
160  for (;;) {
161  // Move First to just past the next ")".
162  First = Last;
163  while (First != End && *First != ')')
164  ++First;
165  if (First == End)
166  return;
167  ++First;
168 
169  // Look ahead for the terminator sequence.
170  Last = First;
171  while (Last != End && size_t(Last - First) < Terminator.size() &&
172  Terminator[Last - First] == *Last)
173  ++Last;
174 
175  // Check if we hit it (or the end of the file).
176  if (Last == End) {
177  First = Last;
178  return;
179  }
180  if (size_t(Last - First) < Terminator.size())
181  continue;
182  if (*Last != '"')
183  continue;
184  First = Last + 1;
185  return;
186  }
187 }
188 
189 // Returns the length of EOL, either 0 (no end-of-line), 1 (\n) or 2 (\r\n)
190 static unsigned isEOL(const char *First, const char *const End) {
191  if (First == End)
192  return 0;
193  if (End - First > 1 && isVerticalWhitespace(First[0]) &&
194  isVerticalWhitespace(First[1]) && First[0] != First[1])
195  return 2;
196  return !!isVerticalWhitespace(First[0]);
197 }
198 
199 static void skipString(const char *&First, const char *const End) {
200  assert(*First == '\'' || *First == '"' || *First == '<');
201  const char Terminator = *First == '<' ? '>' : *First;
202  for (++First; First != End && *First != Terminator; ++First) {
203  // String and character literals don't extend past the end of the line.
205  return;
206  if (*First != '\\')
207  continue;
208  // Skip past backslash to the next character. This ensures that the
209  // character right after it is skipped as well, which matters if it's
210  // the terminator.
211  if (++First == End)
212  return;
213  if (!isWhitespace(*First))
214  continue;
215  // Whitespace after the backslash might indicate a line continuation.
216  const char *FirstAfterBackslashPastSpace = First;
217  skipOverSpaces(FirstAfterBackslashPastSpace, End);
218  if (unsigned NLSize = isEOL(FirstAfterBackslashPastSpace, End)) {
219  // Advance the character pointer to the next line for the next
220  // iteration.
221  First = FirstAfterBackslashPastSpace + NLSize - 1;
222  }
223  }
224  if (First != End)
225  ++First; // Finish off the string.
226 }
227 
228 // Returns the length of the skipped newline
229 static unsigned skipNewline(const char *&First, const char *End) {
230  if (First == End)
231  return 0;
232  assert(isVerticalWhitespace(*First));
233  unsigned Len = isEOL(First, End);
234  assert(Len && "expected newline");
235  First += Len;
236  return Len;
237 }
238 
239 static bool wasLineContinuation(const char *First, unsigned EOLLen) {
240  return *(First - (int)EOLLen - 1) == '\\';
241 }
242 
243 static void skipToNewlineRaw(const char *&First, const char *const End) {
244  for (;;) {
245  if (First == End)
246  return;
247 
248  unsigned Len = isEOL(First, End);
249  if (Len)
250  return;
251 
252  do {
253  if (++First == End)
254  return;
255  Len = isEOL(First, End);
256  } while (!Len);
257 
258  if (First[-1] != '\\')
259  return;
260 
261  First += Len;
262  // Keep skipping lines...
263  }
264 }
265 
266 static const char *findLastNonSpace(const char *First, const char *Last) {
267  assert(First <= Last);
268  while (First != Last && isHorizontalWhitespace(Last[-1]))
269  --Last;
270  return Last;
271 }
272 
273 static const char *findFirstTrailingSpace(const char *First,
274  const char *Last) {
275  const char *LastNonSpace = findLastNonSpace(First, Last);
276  if (Last == LastNonSpace)
277  return Last;
278  assert(isHorizontalWhitespace(LastNonSpace[0]));
279  return LastNonSpace + 1;
280 }
281 
282 static void skipLineComment(const char *&First, const char *const End) {
283  assert(First[0] == '/' && First[1] == '/');
284  First += 2;
286 }
287 
288 static void skipBlockComment(const char *&First, const char *const End) {
289  assert(First[0] == '/' && First[1] == '*');
290  if (End - First < 4) {
291  First = End;
292  return;
293  }
294  for (First += 3; First != End; ++First)
295  if (First[-1] == '*' && First[0] == '/') {
296  ++First;
297  return;
298  }
299 }
300 
301 /// \returns True if the current single quotation mark character is a C++ 14
302 /// digit separator.
303 static bool isQuoteCppDigitSeparator(const char *const Start,
304  const char *const Cur,
305  const char *const End) {
306  assert(*Cur == '\'' && "expected quotation character");
307  // skipLine called in places where we don't expect a valid number
308  // body before `start` on the same line, so always return false at the start.
309  if (Start == Cur)
310  return false;
311  // The previous character must be a valid PP number character.
312  // Make sure that the L, u, U, u8 prefixes don't get marked as a
313  // separator though.
314  char Prev = *(Cur - 1);
315  if (Prev == 'L' || Prev == 'U' || Prev == 'u')
316  return false;
317  if (Prev == '8' && (Cur - 1 != Start) && *(Cur - 2) == 'u')
318  return false;
319  if (!isPreprocessingNumberBody(Prev))
320  return false;
321  // The next character should be a valid identifier body character.
322  return (Cur + 1) < End && isAsciiIdentifierContinue(*(Cur + 1));
323 }
324 
325 static void skipLine(const char *&First, const char *const End) {
326  for (;;) {
327  assert(First <= End);
328  if (First == End)
329  return;
330 
331  if (isVerticalWhitespace(*First)) {
333  return;
334  }
335  const char *Start = First;
336  while (First != End && !isVerticalWhitespace(*First)) {
337  // Iterate over strings correctly to avoid comments and newlines.
338  if (*First == '"' ||
339  (*First == '\'' && !isQuoteCppDigitSeparator(Start, First, End))) {
340  if (isRawStringLiteral(Start, First))
342  else
343  skipString(First, End);
344  continue;
345  }
346 
347  // Iterate over comments correctly.
348  if (*First != '/' || End - First < 2) {
349  ++First;
350  continue;
351  }
352 
353  if (First[1] == '/') {
354  // "//...".
356  continue;
357  }
358 
359  if (First[1] != '*') {
360  ++First;
361  continue;
362  }
363 
364  // "/*...*/".
366  }
367  if (First == End)
368  return;
369 
370  // Skip over the newline.
371  unsigned Len = skipNewline(First, End);
372  if (!wasLineContinuation(First, Len)) // Continue past line-continuations.
373  break;
374  }
375 }
376 
377 static void skipDirective(StringRef Name, const char *&First,
378  const char *const End) {
379  if (llvm::StringSwitch<bool>(Name)
380  .Case("warning", true)
381  .Case("error", true)
382  .Default(false))
383  // Do not process quotes or comments.
385  else
386  skipLine(First, End);
387 }
388 
389 void Minimizer::printToNewline(const char *&First, const char *const End) {
390  while (First != End && !isVerticalWhitespace(*First)) {
391  const char *Last = First;
392  do {
393  // Iterate over strings correctly to avoid comments and newlines.
394  if (*Last == '"' || *Last == '\'' ||
395  (*Last == '<' && top() == pp_include)) {
396  if (LLVM_UNLIKELY(isRawStringLiteral(First, Last)))
397  skipRawString(Last, End);
398  else
399  skipString(Last, End);
400  continue;
401  }
402  if (*Last != '/' || End - Last < 2) {
403  ++Last;
404  continue; // Gather the rest up to print verbatim.
405  }
406 
407  if (Last[1] != '/' && Last[1] != '*') {
408  ++Last;
409  continue;
410  }
411 
412  // Deal with "//..." and "/*...*/".
413  append(First, findFirstTrailingSpace(First, Last));
414  First = Last;
415 
416  if (Last[1] == '/') {
417  skipLineComment(First, End);
418  return;
419  }
420 
421  put(' ');
422  skipBlockComment(First, End);
423  skipOverSpaces(First, End);
424  Last = First;
425  } while (Last != End && !isVerticalWhitespace(*Last));
426 
427  // Print out the string.
428  const char *LastBeforeTrailingSpace = findLastNonSpace(First, Last);
429  if (Last == End || LastBeforeTrailingSpace == First ||
430  LastBeforeTrailingSpace[-1] != '\\') {
431  append(First, LastBeforeTrailingSpace);
432  First = Last;
433  skipNewline(First, End);
434  return;
435  }
436 
437  // Print up to the backslash, backing up over spaces. Preserve at least one
438  // space, as the space matters when tokens are separated by a line
439  // continuation.
440  append(First, findFirstTrailingSpace(
441  First, LastBeforeTrailingSpace - 1));
442 
443  First = Last;
444  skipNewline(First, End);
445  skipOverSpaces(First, End);
446  }
447 }
448 
449 static void skipWhitespace(const char *&First, const char *const End) {
450  for (;;) {
451  assert(First <= End);
453 
454  if (End - First < 2)
455  return;
456 
457  if (First[0] == '\\' && isVerticalWhitespace(First[1])) {
458  skipNewline(++First, End);
459  continue;
460  }
461 
462  // Check for a non-comment character.
463  if (First[0] != '/')
464  return;
465 
466  // "// ...".
467  if (First[1] == '/') {
469  return;
470  }
471 
472  // Cannot be a comment.
473  if (First[1] != '*')
474  return;
475 
476  // "/*...*/".
478  }
479 }
480 
481 void Minimizer::printAdjacentModuleNameParts(const char *&First,
482  const char *const End) {
483  // Skip over parts of the body.
484  const char *Last = First;
485  do
486  ++Last;
487  while (Last != End && (isAsciiIdentifierContinue(*Last) || *Last == '.'));
488  append(First, Last);
489  First = Last;
490 }
491 
492 bool Minimizer::printAtImportBody(const char *&First, const char *const End) {
493  for (;;) {
494  skipWhitespace(First, End);
495  if (First == End)
496  return true;
497 
498  if (isVerticalWhitespace(*First)) {
499  skipNewline(First, End);
500  continue;
501  }
502 
503  // Found a semicolon.
504  if (*First == ';') {
505  put(*First++).put('\n');
506  return false;
507  }
508 
509  // Don't handle macro expansions inside @import for now.
510  if (!isAsciiIdentifierContinue(*First) && *First != '.')
511  return true;
512 
513  printAdjacentModuleNameParts(First, End);
514  }
515 }
516 
517 void Minimizer::printDirectiveBody(const char *&First, const char *const End) {
518  skipWhitespace(First, End); // Skip initial whitespace.
519  printToNewline(First, End);
520  while (Out.back() == ' ')
521  Out.pop_back();
522  put('\n');
523 }
524 
525 LLVM_NODISCARD static const char *lexRawIdentifier(const char *First,
526  const char *const End) {
527  assert(isAsciiIdentifierContinue(*First) && "invalid identifer");
528  const char *Last = First + 1;
529  while (Last != End && isAsciiIdentifierContinue(*Last))
530  ++Last;
531  return Last;
532 }
533 
534 LLVM_NODISCARD static const char *
535 getIdentifierContinuation(const char *First, const char *const End) {
536  if (End - First < 3 || First[0] != '\\' || !isVerticalWhitespace(First[1]))
537  return nullptr;
538 
539  ++First;
541  if (First == End)
542  return nullptr;
543  return isAsciiIdentifierContinue(First[0]) ? First : nullptr;
544 }
545 
546 Minimizer::IdInfo Minimizer::lexIdentifier(const char *First,
547  const char *const End) {
548  const char *Last = lexRawIdentifier(First, End);
549  const char *Next = getIdentifierContinuation(Last, End);
550  if (LLVM_LIKELY(!Next))
551  return IdInfo{Last, StringRef(First, Last - First)};
552 
553  // Slow path, where identifiers are split over lines.
554  SmallVector<char, 64> Id(First, Last);
555  while (Next) {
556  Last = lexRawIdentifier(Next, End);
557  Id.append(Next, Last);
558  Next = getIdentifierContinuation(Last, End);
559  }
560  return IdInfo{
561  Last,
562  SplitIds.try_emplace(StringRef(Id.begin(), Id.size()), 0).first->first()};
563 }
564 
565 void Minimizer::printAdjacentMacroArgs(const char *&First,
566  const char *const End) {
567  // Skip over parts of the body.
568  const char *Last = First;
569  do
570  ++Last;
571  while (Last != End &&
572  (isAsciiIdentifierContinue(*Last) || *Last == '.' || *Last == ','));
573  append(First, Last);
574  First = Last;
575 }
576 
577 bool Minimizer::printMacroArgs(const char *&First, const char *const End) {
578  assert(*First == '(');
579  put(*First++);
580  for (;;) {
581  skipWhitespace(First, End);
582  if (First == End)
583  return true;
584 
585  if (*First == ')') {
586  put(*First++);
587  return false;
588  }
589 
590  // This is intentionally fairly liberal.
591  if (!(isAsciiIdentifierContinue(*First) || *First == '.' || *First == ','))
592  return true;
593 
594  printAdjacentMacroArgs(First, End);
595  }
596 }
597 
598 /// Looks for an identifier starting from Last.
599 ///
600 /// Updates "First" to just past the next identifier, if any. Returns true iff
601 /// the identifier matches "Id".
602 bool Minimizer::isNextIdentifier(StringRef Id, const char *&First,
603  const char *const End) {
604  skipWhitespace(First, End);
605  if (First == End || !isAsciiIdentifierStart(*First))
606  return false;
607 
608  IdInfo FoundId = lexIdentifier(First, End);
609  First = FoundId.Last;
610  return FoundId.Name == Id;
611 }
612 
613 bool Minimizer::lexAt(const char *&First, const char *const End) {
614  // Handle "@import".
615  const char *ImportLoc = First++;
616  if (!isNextIdentifier("import", First, End)) {
617  skipLine(First, End);
618  return false;
619  }
620  makeToken(decl_at_import);
621  append("@import ");
622  if (printAtImportBody(First, End))
623  return reportError(
624  ImportLoc, diag::err_dep_source_minimizer_missing_sema_after_at_import);
625  skipWhitespace(First, End);
626  if (First == End)
627  return false;
628  if (!isVerticalWhitespace(*First))
629  return reportError(
630  ImportLoc, diag::err_dep_source_minimizer_unexpected_tokens_at_import);
631  skipNewline(First, End);
632  return false;
633 }
634 
635 bool Minimizer::lexModule(const char *&First, const char *const End) {
636  IdInfo Id = lexIdentifier(First, End);
637  First = Id.Last;
638  bool Export = false;
639  if (Id.Name == "export") {
640  Export = true;
641  skipWhitespace(First, End);
642  if (!isAsciiIdentifierContinue(*First)) {
643  skipLine(First, End);
644  return false;
645  }
646  Id = lexIdentifier(First, End);
647  First = Id.Last;
648  }
649 
650  if (Id.Name != "module" && Id.Name != "import") {
651  skipLine(First, End);
652  return false;
653  }
654 
655  skipWhitespace(First, End);
656 
657  // Ignore this as a module directive if the next character can't be part of
658  // an import.
659 
660  switch (*First) {
661  case ':':
662  case '<':
663  case '"':
664  break;
665  default:
666  if (!isAsciiIdentifierContinue(*First)) {
667  skipLine(First, End);
668  return false;
669  }
670  }
671 
672  if (Export) {
673  makeToken(cxx_export_decl);
674  append("export ");
675  }
676 
677  if (Id.Name == "module")
678  makeToken(cxx_module_decl);
679  else
680  makeToken(cxx_import_decl);
681  append(Id.Name);
682  append(" ");
683  printToNewline(First, End);
684  append("\n");
685  return false;
686 }
687 
688 bool Minimizer::lexDefine(const char *&First, const char *const End) {
689  makeToken(pp_define);
690  append("#define ");
691  skipWhitespace(First, End);
692 
693  if (!isAsciiIdentifierStart(*First))
694  return reportError(First, diag::err_pp_macro_not_identifier);
695 
696  IdInfo Id = lexIdentifier(First, End);
697  const char *Last = Id.Last;
698  append(Id.Name);
699  if (Last == End)
700  return false;
701  if (*Last == '(') {
702  size_t Size = Out.size();
703  if (printMacroArgs(Last, End)) {
704  // Be robust to bad macro arguments, since they can show up in disabled
705  // code.
706  Out.resize(Size);
707  append("(/* invalid */\n");
708  skipLine(Last, End);
709  return false;
710  }
711  }
712  skipWhitespace(Last, End);
713  if (Last == End)
714  return false;
715  if (!isVerticalWhitespace(*Last))
716  put(' ');
717  printDirectiveBody(Last, End);
718  First = Last;
719  return false;
720 }
721 
722 bool Minimizer::lexPragma(const char *&First, const char *const End) {
723  // #pragma.
724  skipWhitespace(First, End);
725  if (First == End || !isAsciiIdentifierStart(*First))
726  return false;
727 
728  IdInfo FoundId = lexIdentifier(First, End);
729  First = FoundId.Last;
730  if (FoundId.Name == "once") {
731  // #pragma once
732  skipLine(First, End);
733  makeToken(pp_pragma_once);
734  append("#pragma once\n");
735  return false;
736  }
737  if (FoundId.Name == "push_macro") {
738  // #pragma push_macro
739  makeToken(pp_pragma_push_macro);
740  append("#pragma push_macro");
741  printDirectiveBody(First, End);
742  return false;
743  }
744  if (FoundId.Name == "pop_macro") {
745  // #pragma pop_macro
746  makeToken(pp_pragma_pop_macro);
747  append("#pragma pop_macro");
748  printDirectiveBody(First, End);
749  return false;
750  }
751  if (FoundId.Name == "include_alias") {
752  // #pragma include_alias
753  makeToken(pp_pragma_include_alias);
754  append("#pragma include_alias");
755  printDirectiveBody(First, End);
756  return false;
757  }
758 
759  if (FoundId.Name != "clang") {
760  skipLine(First, End);
761  return false;
762  }
763 
764  // #pragma clang.
765  if (!isNextIdentifier("module", First, End)) {
766  skipLine(First, End);
767  return false;
768  }
769 
770  // #pragma clang module.
771  if (!isNextIdentifier("import", First, End)) {
772  skipLine(First, End);
773  return false;
774  }
775 
776  // #pragma clang module import.
777  makeToken(pp_pragma_import);
778  append("#pragma clang module import ");
779  printDirectiveBody(First, End);
780  return false;
781 }
782 
783 bool Minimizer::lexEndif(const char *&First, const char *const End) {
784  // Strip out "#else" if it's empty.
785  if (top() == pp_else)
786  popToken();
787 
788  // If "#ifdef" is empty, strip it and skip the "#endif".
789  //
790  // FIXME: Once/if Clang starts disallowing __has_include in macro expansions,
791  // we can skip empty `#if` and `#elif` blocks as well after scanning for a
792  // literal __has_include in the condition. Even without that rule we could
793  // drop the tokens if we scan for identifiers in the condition and find none.
794  if (top() == pp_ifdef || top() == pp_ifndef) {
795  popToken();
796  skipLine(First, End);
797  return false;
798  }
799 
800  return lexDefault(pp_endif, "endif", First, End);
801 }
802 
803 bool Minimizer::lexDefault(TokenKind Kind, StringRef Directive,
804  const char *&First, const char *const End) {
805  makeToken(Kind);
806  put('#').append(Directive).put(' ');
807  printDirectiveBody(First, End);
808  return false;
809 }
810 
811 static bool isStartOfRelevantLine(char First) {
812  switch (First) {
813  case '#':
814  case '@':
815  case 'i':
816  case 'e':
817  case 'm':
818  return true;
819  }
820  return false;
821 }
822 
823 bool Minimizer::lexPPLine(const char *&First, const char *const End) {
824  assert(First != End);
825 
826  skipWhitespace(First, End);
827  assert(First <= End);
828  if (First == End)
829  return false;
830 
831  if (!isStartOfRelevantLine(*First)) {
832  skipLine(First, End);
833  assert(First <= End);
834  return false;
835  }
836 
837  // Handle "@import".
838  if (*First == '@')
839  return lexAt(First, End);
840 
841  if (*First == 'i' || *First == 'e' || *First == 'm')
842  return lexModule(First, End);
843 
844  // Handle preprocessing directives.
845  ++First; // Skip over '#'.
846  skipWhitespace(First, End);
847 
848  if (First == End)
849  return reportError(First, diag::err_pp_expected_eol);
850 
851  if (!isAsciiIdentifierStart(*First)) {
852  skipLine(First, End);
853  return false;
854  }
855 
856  // Figure out the token.
857  IdInfo Id = lexIdentifier(First, End);
858  First = Id.Last;
859 
860  if (Id.Name == "pragma")
861  return lexPragma(First, End);
862 
863  auto Kind = llvm::StringSwitch<TokenKind>(Id.Name)
864  .Case("include", pp_include)
865  .Case("__include_macros", pp___include_macros)
866  .Case("define", pp_define)
867  .Case("undef", pp_undef)
868  .Case("import", pp_import)
869  .Case("include_next", pp_include_next)
870  .Case("if", pp_if)
871  .Case("ifdef", pp_ifdef)
872  .Case("ifndef", pp_ifndef)
873  .Case("elif", pp_elif)
874  .Case("elifdef", pp_elifdef)
875  .Case("elifndef", pp_elifndef)
876  .Case("else", pp_else)
877  .Case("endif", pp_endif)
878  .Default(pp_none);
879  if (Kind == pp_none) {
880  skipDirective(Id.Name, First, End);
881  return false;
882  }
883 
884  if (Kind == pp_endif)
885  return lexEndif(First, End);
886 
887  if (Kind == pp_define)
888  return lexDefine(First, End);
889 
890  // Everything else.
891  return lexDefault(Kind, Id.Name, First, End);
892 }
893 
894 static void skipUTF8ByteOrderMark(const char *&First, const char *const End) {
895  if ((End - First) >= 3 && First[0] == '\xef' && First[1] == '\xbb' &&
896  First[2] == '\xbf')
897  First += 3;
898 }
899 
900 bool Minimizer::minimizeImpl(const char *First, const char *const End) {
901  skipUTF8ByteOrderMark(First, End);
902  while (First != End)
903  if (lexPPLine(First, End))
904  return true;
905  return false;
906 }
907 
908 bool Minimizer::minimize() {
909  bool Error = minimizeImpl(Input.begin(), Input.end());
910 
911  if (!Error) {
912  // Add a trailing newline and an EOF on success.
913  if (!Out.empty() && Out.back() != '\n')
914  Out.push_back('\n');
915  makeToken(pp_eof);
916  }
917 
918  // Null-terminate the output. This way the memory buffer that's passed to
919  // Clang will not have to worry about the terminating '\0'.
920  Out.push_back(0);
921  Out.pop_back();
922  return Error;
923 }
924 
927  struct Directive {
928  enum DirectiveKind {
929  If, // if/ifdef/ifndef
930  Else // elif/elifdef/elifndef, else
931  };
932  int Offset;
933  DirectiveKind Kind;
934  };
936  for (const Token &T : Input) {
937  switch (T.K) {
938  case pp_if:
939  case pp_ifdef:
940  case pp_ifndef:
941  Offsets.push_back({T.Offset, Directive::If});
942  break;
943 
944  case pp_elif:
945  case pp_elifdef:
946  case pp_elifndef:
947  case pp_else: {
948  if (Offsets.empty())
949  return true;
950  int PreviousOffset = Offsets.back().Offset;
951  Range.push_back({PreviousOffset, T.Offset - PreviousOffset});
952  Offsets.push_back({T.Offset, Directive::Else});
953  break;
954  }
955 
956  case pp_endif: {
957  if (Offsets.empty())
958  return true;
959  int PreviousOffset = Offsets.back().Offset;
960  Range.push_back({PreviousOffset, T.Offset - PreviousOffset});
961  do {
962  Directive::DirectiveKind Kind = Offsets.pop_back_val().Kind;
963  if (Kind == Directive::If)
964  break;
965  } while (!Offsets.empty());
966  break;
967  }
968  default:
969  break;
970  }
971  }
972  return false;
973 }
974 
976  StringRef Input, SmallVectorImpl<char> &Output,
978  SourceLocation InputSourceLoc) {
979  Output.clear();
980  Tokens.clear();
981  return Minimizer(Output, Tokens, Input, Diags, InputSourceLoc).minimize();
982 }
clang::minimize_source_to_dependency_directives::pp_pragma_push_macro
@ pp_pragma_push_macro
Definition: DependencyDirectivesSourceMinimizer.h:42
skipString
static void skipString(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:199
clang::minimize_source_to_dependency_directives::pp_ifdef
@ pp_ifdef
Definition: DependencyDirectivesSourceMinimizer.h:47
clang::minimize_source_to_dependency_directives::decl_at_import
@ decl_at_import
Definition: DependencyDirectivesSourceMinimizer.h:54
skipOverSpaces
static void skipOverSpaces(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:117
llvm
Definition: Dominators.h:30
skipLine
static void skipLine(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:325
clang::minimize_source_to_dependency_directives::pp_elifdef
@ pp_elifdef
Definition: DependencyDirectivesSourceMinimizer.h:50
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
clang::minimize_source_to_dependency_directives::pp_pragma_include_alias
@ pp_pragma_include_alias
Definition: DependencyDirectivesSourceMinimizer.h:44
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::minimizeSourceToDependencyDirectives
bool minimizeSourceToDependencyDirectives(llvm::StringRef Input, llvm::SmallVectorImpl< char > &Output, llvm::SmallVectorImpl< minimize_source_to_dependency_directives::Token > &Tokens, DiagnosticsEngine *Diags=nullptr, SourceLocation InputSourceLoc=SourceLocation())
Minimize the input down to the preprocessor directives that might have an effect on the dependencies ...
clang::minimize_source_to_dependency_directives::pp_eof
@ pp_eof
Definition: DependencyDirectivesSourceMinimizer.h:58
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::minimize_source_to_dependency_directives::cxx_module_decl
@ cxx_module_decl
Definition: DependencyDirectivesSourceMinimizer.h:56
int
__device__ int
Definition: __clang_hip_libdevice_declares.h:63
clang::minimize_source_to_dependency_directives::pp_ifndef
@ pp_ifndef
Definition: DependencyDirectivesSourceMinimizer.h:48
clang::ComparisonCategoryType::First
@ First
clang::minimize_source_to_dependency_directives::pp_import
@ pp_import
Definition: DependencyDirectivesSourceMinimizer.h:39
clang::minimize_source_to_dependency_directives::pp_define
@ pp_define
Definition: DependencyDirectivesSourceMinimizer.h:37
clang::minimize_source_to_dependency_directives::cxx_export_decl
@ cxx_export_decl
Definition: DependencyDirectivesSourceMinimizer.h:55
clang::minimize_source_to_dependency_directives::pp_pragma_import
@ pp_pragma_import
Definition: DependencyDirectivesSourceMinimizer.h:40
End
SourceLocation End
Definition: USRLocFinder.cpp:167
clang::isPreprocessingNumberBody
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
Definition: CharInfo.h:152
Offset
unsigned Offset
Definition: Format.cpp:2381
clang::minimize_source_to_dependency_directives::pp_if
@ pp_if
Definition: DependencyDirectivesSourceMinimizer.h:46
clang::StructuralEquivalenceKind::Default
@ Default
skipNewline
static unsigned skipNewline(const char *&First, const char *End)
Definition: DependencyDirectivesSourceMinimizer.cpp:229
clang::minimize_source_to_dependency_directives::cxx_import_decl
@ cxx_import_decl
Definition: DependencyDirectivesSourceMinimizer.h:57
clang::minimize_source_to_dependency_directives::pp_include_next
@ pp_include_next
Definition: DependencyDirectivesSourceMinimizer.h:45
skipUTF8ByteOrderMark
static void skipUTF8ByteOrderMark(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:894
Id
int Id
Definition: ASTDiff.cpp:191
findFirstTrailingSpace
static const char * findFirstTrailingSpace(const char *First, const char *Last)
Definition: DependencyDirectivesSourceMinimizer.cpp:273
Diagnostic.h
LexDiagnostic.h
isEOL
static unsigned isEOL(const char *First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:190
isStartOfRelevantLine
static bool isStartOfRelevantLine(char First)
Definition: DependencyDirectivesSourceMinimizer.cpp:811
isRawStringLiteral
static LLVM_NODISCARD bool isRawStringLiteral(const char *First, const char *Current)
Definition: DependencyDirectivesSourceMinimizer.cpp:122
skipRawString
static void skipRawString(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:147
clang::minimize_source_to_dependency_directives::pp_include
@ pp_include
Definition: DependencyDirectivesSourceMinimizer.h:35
clang::minimize_source_to_dependency_directives::pp_elifndef
@ pp_elifndef
Definition: DependencyDirectivesSourceMinimizer.h:51
skipDirective
static void skipDirective(StringRef Name, const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:377
clang::minimize_source_to_dependency_directives::pp_else
@ pp_else
Definition: DependencyDirectivesSourceMinimizer.h:52
clang::minimize_source_to_dependency_directives::pp_none
@ pp_none
Definition: DependencyDirectivesSourceMinimizer.h:34
clang::isAsciiIdentifierContinue
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c, bool AllowDollar=false)
Returns true if this is a body character of a C identifier, which is [a-zA-Z0-9_].
Definition: CharInfo.h:63
clang::minimize_source_to_dependency_directives::pp_pragma_pop_macro
@ pp_pragma_pop_macro
Definition: DependencyDirectivesSourceMinimizer.h:43
findLastNonSpace
static const char * findLastNonSpace(const char *First, const char *Last)
Definition: DependencyDirectivesSourceMinimizer.cpp:266
getIdentifierContinuation
static const LLVM_NODISCARD char * getIdentifierContinuation(const char *First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:535
skipLineComment
static void skipLineComment(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:282
clang::isWhitespace
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
Definition: CharInfo.h:92
CharInfo.h
clang::syntax::NodeRole::Size
@ Size
clang::minimize_source_to_dependency_directives::pp_undef
@ pp_undef
Definition: DependencyDirectivesSourceMinimizer.h:38
clang::minimize_source_to_dependency_directives::computeSkippedRanges
bool computeSkippedRanges(ArrayRef< Token > Input, llvm::SmallVectorImpl< SkippedRange > &Range)
Computes the potential source ranges that can be skipped by the preprocessor when skipping a directiv...
Definition: DependencyDirectivesSourceMinimizer.cpp:925
skipWhitespace
static void skipWhitespace(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:449
wasLineContinuation
static bool wasLineContinuation(const char *First, unsigned EOLLen)
Definition: DependencyDirectivesSourceMinimizer.cpp:239
lexRawIdentifier
static const LLVM_NODISCARD char * lexRawIdentifier(const char *First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:525
llvm::ArrayRef
Definition: LLVM.h:34
clang::minimize_source_to_dependency_directives::Token
Represents a simplified token that's lexed as part of the source minimization.
Definition: DependencyDirectivesSourceMinimizer.h:64
skipBlockComment
static void skipBlockComment(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:288
clang::minimize_source_to_dependency_directives
Definition: DependencyDirectivesSourceMinimizer.h:29
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::isVerticalWhitespace
LLVM_READONLY bool isVerticalWhitespace(unsigned char c)
Returns true if this character is vertical ASCII whitespace: '\n', '\r'.
Definition: CharInfo.h:83
clang
Definition: CalledOnceCheck.h:17
clang::minimize_source_to_dependency_directives::pp___include_macros
@ pp___include_macros
Definition: DependencyDirectivesSourceMinimizer.h:36
clang::ComparisonCategoryType::Last
@ Last
skipToNewlineRaw
static void skipToNewlineRaw(const char *&First, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:243
clang::minimize_source_to_dependency_directives::pp_endif
@ pp_endif
Definition: DependencyDirectivesSourceMinimizer.h:53
clang::VerifyDiagnosticConsumer::Directive
Directive - Abstract class representing a parsed verify directive.
Definition: VerifyDiagnosticConsumer.h:191
clang::isHorizontalWhitespace
LLVM_READONLY bool isHorizontalWhitespace(unsigned char c)
Returns true if this character is horizontal ASCII whitespace: ' ', '\t', '\f', '\v'.
Definition: CharInfo.h:75
isQuoteCppDigitSeparator
static bool isQuoteCppDigitSeparator(const char *const Start, const char *const Cur, const char *const End)
Definition: DependencyDirectivesSourceMinimizer.cpp:303
llvm::SmallVectorImpl< char >
clang::isAsciiIdentifierStart
LLVM_READONLY bool isAsciiIdentifierStart(unsigned char c, bool AllowDollar=false)
Returns true if this is a valid first character of a C identifier, which is [a-zA-Z_].
Definition: CharInfo.h:53
clang::minimize_source_to_dependency_directives::TokenKind
TokenKind
Represents the kind of preprocessor directive or a module declaration that is tracked by the source m...
Definition: DependencyDirectivesSourceMinimizer.h:33
clang::minimize_source_to_dependency_directives::pp_pragma_once
@ pp_pragma_once
Definition: DependencyDirectivesSourceMinimizer.h:41
clang::minimize_source_to_dependency_directives::pp_elif
@ pp_elif
Definition: DependencyDirectivesSourceMinimizer.h:49
DependencyDirectivesSourceMinimizer.h