| 1 |
//===- DependencyDirectivesScanner.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 interface for scanning header and source files to get 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 |
|
| 17 |
#include "clang/Lex/DependencyDirectivesScanner.h" |
| 18 |
#include "clang/Basic/CharInfo.h" |
| 19 |
#include "clang/Basic/Diagnostic.h" |
| 20 |
#include "clang/Lex/LexDiagnostic.h" |
| 21 |
#include "clang/Lex/Lexer.h" |
| 22 |
#include "clang/Lex/Pragma.h" |
| 23 |
#include "llvm/ADT/ScopeExit.h" |
| 24 |
#include "llvm/ADT/SmallString.h" |
| 25 |
#include "llvm/ADT/StringMap.h" |
| 26 |
#include "llvm/ADT/StringSwitch.h" |
| 27 |
#include <optional> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
using namespace clang::dependency_directives_scan; |
| 31 |
using namespace llvm; |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
struct DirectiveWithTokens { |
| 36 |
DirectiveKind Kind; |
| 37 |
unsigned NumTokens; |
| 38 |
|
| 39 |
DirectiveWithTokens(DirectiveKind Kind, unsigned NumTokens) |
| 40 |
: Kind(Kind), NumTokens(NumTokens) {} |
| 41 |
}; |
| 42 |
|
| 43 |
/// Does an efficient "scan" of the sources to detect the presence of |
| 44 |
/// preprocessor (or module import) directives and collects the raw lexed tokens |
| 45 |
/// for those directives so that the \p Lexer can "replay" them when the file is |
| 46 |
/// included. |
| 47 |
/// |
| 48 |
/// Note that the behavior of the raw lexer is affected by the language mode, |
| 49 |
/// while at this point we want to do a scan and collect tokens once, |
| 50 |
/// irrespective of the language mode that the file will get included in. To |
| 51 |
/// compensate for that the \p Lexer, while "replaying", will adjust a token |
| 52 |
/// where appropriate, when it could affect the preprocessor's state. |
| 53 |
/// For example in a directive like |
| 54 |
/// |
| 55 |
/// \code |
| 56 |
/// #if __has_cpp_attribute(clang::fallthrough) |
| 57 |
/// \endcode |
| 58 |
/// |
| 59 |
/// The preprocessor needs to see '::' as 'tok::coloncolon' instead of 2 |
| 60 |
/// 'tok::colon'. The \p Lexer will adjust if it sees consecutive 'tok::colon' |
| 61 |
/// while in C++ mode. |
| 62 |
struct Scanner { |
| 63 |
Scanner(StringRef Input, |
| 64 |
SmallVectorImpl<dependency_directives_scan::Token> &Tokens, |
| 65 |
DiagnosticsEngine *Diags, SourceLocation InputSourceLoc) |
| 66 |
: Input(Input), Tokens(Tokens), Diags(Diags), |
| 67 |
InputSourceLoc(InputSourceLoc), LangOpts(getLangOptsForDepScanning()), |
| 68 |
TheLexer(InputSourceLoc, LangOpts, Input.begin(), Input.begin(), |
| 69 |
Input.end()) {} |
| 70 |
|
| 71 |
static LangOptions getLangOptsForDepScanning() { |
| 72 |
LangOptions LangOpts; |
| 73 |
// Set the lexer to use 'tok::at' for '@', instead of 'tok::unknown'. |
| 74 |
LangOpts.ObjC = true; |
| 75 |
LangOpts.LineComment = true; |
| 76 |
LangOpts.RawStringLiterals = true; |
| 77 |
// FIXME: we do not enable C11 or C++11, so we are missing u/u8/U"". |
| 78 |
return LangOpts; |
| 79 |
} |
| 80 |
|
| 81 |
/// Lex the provided source and emit the directive tokens. |
| 82 |
/// |
| 83 |
/// \returns True on error. |
| 84 |
bool scan(SmallVectorImpl<Directive> &Directives); |
| 85 |
|
| 86 |
private: |
| 87 |
/// Lexes next token and advances \p First and the \p Lexer. |
| 88 |
[[nodiscard]] dependency_directives_scan::Token & |
| 89 |
lexToken(const char *&First, const char *const End); |
| 90 |
|
| 91 |
[[nodiscard]] dependency_directives_scan::Token & |
| 92 |
lexIncludeFilename(const char *&First, const char *const End); |
| 93 |
|
| 94 |
void skipLine(const char *&First, const char *const End); |
| 95 |
void skipDirective(StringRef Name, const char *&First, const char *const End); |
| 96 |
|
| 97 |
/// Returns the spelling of a string literal or identifier after performing |
| 98 |
/// any processing needed to handle \c clang::Token::NeedsCleaning. |
| 99 |
StringRef cleanStringIfNeeded(const dependency_directives_scan::Token &Tok); |
| 100 |
|
| 101 |
/// Lexes next token and if it is identifier returns its string, otherwise |
| 102 |
/// it skips the current line and returns \p std::nullopt. |
| 103 |
/// |
| 104 |
/// In any case (whatever the token kind) \p First and the \p Lexer will |
| 105 |
/// advance beyond the token. |
| 106 |
[[nodiscard]] std::optional<StringRef> |
| 107 |
tryLexIdentifierOrSkipLine(const char *&First, const char *const End); |
| 108 |
|
| 109 |
/// Used when it is certain that next token is an identifier. |
| 110 |
[[nodiscard]] StringRef lexIdentifier(const char *&First, |
| 111 |
const char *const End); |
| 112 |
|
| 113 |
/// Lexes next token and returns true iff it is an identifier that matches \p |
| 114 |
/// Id, otherwise it skips the current line and returns false. |
| 115 |
/// |
| 116 |
/// In any case (whatever the token kind) \p First and the \p Lexer will |
| 117 |
/// advance beyond the token. |
| 118 |
[[nodiscard]] bool isNextIdentifierOrSkipLine(StringRef Id, |
| 119 |
const char *&First, |
| 120 |
const char *const End); |
| 121 |
|
| 122 |
/// Lexes next token and returns true iff it matches the kind \p K. |
| 123 |
/// Otherwise it skips the current line and returns false. |
| 124 |
/// |
| 125 |
/// In any case (whatever the token kind) \p First and the \p Lexer will |
| 126 |
/// advance beyond the token. |
| 127 |
[[nodiscard]] bool isNextTokenOrSkipLine(tok::TokenKind K, const char *&First, |
| 128 |
const char *const End); |
| 129 |
|
| 130 |
/// Lexes next token and if it is string literal, returns its string. |
| 131 |
/// Otherwise, it skips the current line and returns \p std::nullopt. |
| 132 |
/// |
| 133 |
/// In any case (whatever the token kind) \p First and the \p Lexer will |
| 134 |
/// advance beyond the token. |
| 135 |
[[nodiscard]] std::optional<StringRef> |
| 136 |
tryLexStringLiteralOrSkipLine(const char *&First, const char *const End); |
| 137 |
|
| 138 |
[[nodiscard]] bool scanImpl(const char *First, const char *const End); |
| 139 |
[[nodiscard]] bool lexPPLine(const char *&First, const char *const End); |
| 140 |
[[nodiscard]] bool lexAt(const char *&First, const char *const End); |
| 141 |
[[nodiscard]] bool lexModule(const char *&First, const char *const End); |
| 142 |
[[nodiscard]] bool lexDefine(const char *HashLoc, const char *&First, |
| 143 |
const char *const End); |
| 144 |
[[nodiscard]] bool lexPragma(const char *&First, const char *const End); |
| 145 |
[[nodiscard]] bool lex_Pragma(const char *&First, const char *const End); |
| 146 |
[[nodiscard]] bool lexEndif(const char *&First, const char *const End); |
| 147 |
[[nodiscard]] bool lexDefault(DirectiveKind Kind, const char *&First, |
| 148 |
const char *const End); |
| 149 |
[[nodiscard]] bool lexModuleDirectiveBody(DirectiveKind Kind, |
| 150 |
const char *&First, |
| 151 |
const char *const End); |
| 152 |
void lexPPDirectiveBody(const char *&First, const char *const End); |
| 153 |
|
| 154 |
DirectiveWithTokens &pushDirective(DirectiveKind Kind) { |
| 155 |
Tokens.append(CurDirToks); |
| 156 |
DirsWithToks.emplace_back(Kind, CurDirToks.size()); |
| 157 |
CurDirToks.clear(); |
| 158 |
return DirsWithToks.back(); |
| 159 |
} |
| 160 |
void popDirective() { |
| 161 |
Tokens.pop_back_n(DirsWithToks.pop_back_val().NumTokens); |
| 162 |
} |
| 163 |
DirectiveKind topDirective() const { |
| 164 |
return DirsWithToks.empty() ? pp_none : DirsWithToks.back().Kind; |
| 165 |
} |
| 166 |
|
| 167 |
unsigned getOffsetAt(const char *CurPtr) const { |
| 168 |
return CurPtr - Input.data(); |
| 169 |
} |
| 170 |
|
| 171 |
/// Reports a diagnostic if the diagnostic engine is provided. Always returns |
| 172 |
/// true at the end. |
| 173 |
bool reportError(const char *CurPtr, unsigned Err); |
| 174 |
|
| 175 |
StringMap<char> SplitIds; |
| 176 |
StringRef Input; |
| 177 |
SmallVectorImpl<dependency_directives_scan::Token> &Tokens; |
| 178 |
DiagnosticsEngine *Diags; |
| 179 |
SourceLocation InputSourceLoc; |
| 180 |
|
| 181 |
const char *LastTokenPtr = nullptr; |
| 182 |
/// Keeps track of the tokens for the currently lexed directive. Once a |
| 183 |
/// directive is fully lexed and "committed" then the tokens get appended to |
| 184 |
/// \p Tokens and \p CurDirToks is cleared for the next directive. |
| 185 |
SmallVector<dependency_directives_scan::Token, 32> CurDirToks; |
| 186 |
/// The directives that were lexed along with the number of tokens that each |
| 187 |
/// directive contains. The tokens of all the directives are kept in \p Tokens |
| 188 |
/// vector, in the same order as the directives order in \p DirsWithToks. |
| 189 |
SmallVector<DirectiveWithTokens, 64> DirsWithToks; |
| 190 |
LangOptions LangOpts; |
| 191 |
Lexer TheLexer; |
| 192 |
}; |
| 193 |
|
| 194 |
} // end anonymous namespace |
| 195 |
|
| 196 |
bool Scanner::reportError(const char *CurPtr, unsigned Err) { |
| 197 |
if (!Diags) |
| 198 |
return true; |
| 199 |
assert(CurPtr >= Input.data() && "invalid buffer ptr"); |
| 200 |
Diags->Report(InputSourceLoc.getLocWithOffset(getOffsetAt(CurPtr)), Err); |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
static void skipOverSpaces(const char *&First, const char *const End) { |
| 205 |
while (First != End && isHorizontalWhitespace(*First)) |
| 206 |
++First; |
| 207 |
} |
| 208 |
|
| 209 |
// Move back by one character, skipping escaped newlines (backslash + \n) |
| 210 |
static char previousChar(const char *First, const char *&Current) { |
| 211 |
assert(Current > First); |
| 212 |
--Current; |
| 213 |
while (Current > First && isVerticalWhitespace(*Current)) { |
| 214 |
// Check if the previous character is a backslash |
| 215 |
if (Current > First && *(Current - 1) == '\\') { |
| 216 |
// Use Lexer's getEscapedNewLineSize to get the size of the escaped |
| 217 |
// newline |
| 218 |
unsigned EscapeSize = Lexer::getEscapedNewLineSize(Current); |
| 219 |
if (EscapeSize > 0) { |
| 220 |
// Skip back over the entire escaped newline sequence (backslash + |
| 221 |
// newline) |
| 222 |
Current -= (1 + EscapeSize); |
| 223 |
} else { |
| 224 |
break; |
| 225 |
} |
| 226 |
} else { |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
return *Current; |
| 231 |
} |
| 232 |
|
| 233 |
[[nodiscard]] static bool isRawStringLiteral(const char *First, |
| 234 |
const char *Current) { |
| 235 |
assert(First <= Current); |
| 236 |
|
| 237 |
// Check if we can even back up. |
| 238 |
if (*Current != '"' || First == Current) |
| 239 |
return false; |
| 240 |
|
| 241 |
// Check for an "R". |
| 242 |
if (previousChar(First, Current) != 'R') |
| 243 |
return false; |
| 244 |
if (First == Current || |
| 245 |
!isAsciiIdentifierContinue(previousChar(First, Current))) |
| 246 |
return true; |
| 247 |
|
| 248 |
// Check for a prefix of "u", "U", or "L". |
| 249 |
if (*Current == 'u' || *Current == 'U' || *Current == 'L') |
| 250 |
return First == Current || |
| 251 |
!isAsciiIdentifierContinue(previousChar(First, Current)); |
| 252 |
|
| 253 |
// Check for a prefix of "u8". |
| 254 |
if (*Current != '8' || First == Current || |
| 255 |
previousChar(First, Current) != 'u') |
| 256 |
return false; |
| 257 |
return First == Current || |
| 258 |
!isAsciiIdentifierContinue(previousChar(First, Current)); |
| 259 |
} |
| 260 |
|
| 261 |
static void skipRawString(const char *&First, const char *const End) { |
| 262 |
assert(First[0] == '"'); |
| 263 |
|
| 264 |
const char *Last = ++First; |
| 265 |
while (Last != End && *Last != '(') |
| 266 |
++Last; |
| 267 |
if (Last == End) { |
| 268 |
First = Last; // Hit the end... just give up. |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
StringRef Terminator(First, Last - First); |
| 273 |
for (;;) { |
| 274 |
// Move First to just past the next ")". |
| 275 |
First = Last; |
| 276 |
while (First != End && *First != ')') |
| 277 |
++First; |
| 278 |
if (First == End) |
| 279 |
return; |
| 280 |
++First; |
| 281 |
|
| 282 |
// Look ahead for the terminator sequence. |
| 283 |
Last = First; |
| 284 |
while (Last != End && size_t(Last - First) < Terminator.size() && |
| 285 |
Terminator[Last - First] == *Last) |
| 286 |
++Last; |
| 287 |
|
| 288 |
// Check if we hit it (or the end of the file). |
| 289 |
if (Last == End) { |
| 290 |
First = Last; |
| 291 |
return; |
| 292 |
} |
| 293 |
if (size_t(Last - First) < Terminator.size()) |
| 294 |
continue; |
| 295 |
if (*Last != '"') |
| 296 |
continue; |
| 297 |
First = Last + 1; |
| 298 |
return; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Returns the length of EOL, either 0 (no end-of-line), 1 (\n) or 2 (\r\n) |
| 303 |
static unsigned isEOL(const char *First, const char *const End) { |
| 304 |
if (First == End) |
| 305 |
return 0; |
| 306 |
if (End - First > 1 && isVerticalWhitespace(First[0]) && |
| 307 |
isVerticalWhitespace(First[1]) && First[0] != First[1]) |
| 308 |
return 2; |
| 309 |
return !!isVerticalWhitespace(First[0]); |
| 310 |
} |
| 311 |
|
| 312 |
static void skipString(const char *&First, const char *const End) { |
| 313 |
assert(*First == '\'' || *First == '"' || *First == '<'); |
| 314 |
const char Terminator = *First == '<' ? '>' : *First; |
| 315 |
for (++First; First != End && *First != Terminator; ++First) { |
| 316 |
// String and character literals don't extend past the end of the line. |
| 317 |
if (isVerticalWhitespace(*First)) |
| 318 |
return; |
| 319 |
if (*First != '\\') |
| 320 |
continue; |
| 321 |
// Skip past backslash to the next character. This ensures that the |
| 322 |
// character right after it is skipped as well, which matters if it's |
| 323 |
// the terminator. |
| 324 |
if (++First == End) |
| 325 |
return; |
| 326 |
if (!isWhitespace(*First)) |
| 327 |
continue; |
| 328 |
// Whitespace after the backslash might indicate a line continuation. |
| 329 |
const char *FirstAfterBackslashPastSpace = First; |
| 330 |
skipOverSpaces(FirstAfterBackslashPastSpace, End); |
| 331 |
if (unsigned NLSize = isEOL(FirstAfterBackslashPastSpace, End)) { |
| 332 |
// Advance the character pointer to the next line for the next |
| 333 |
// iteration. |
| 334 |
First = FirstAfterBackslashPastSpace + NLSize - 1; |
| 335 |
} |
| 336 |
} |
| 337 |
if (First != End) |
| 338 |
++First; // Finish off the string. |
| 339 |
} |
| 340 |
|
| 341 |
// Returns the length of the skipped newline |
| 342 |
static unsigned skipNewline(const char *&First, const char *End) { |
| 343 |
if (First == End) |
| 344 |
return 0; |
| 345 |
assert(isVerticalWhitespace(*First)); |
| 346 |
unsigned Len = isEOL(First, End); |
| 347 |
assert(Len && "expected newline"); |
| 348 |
First += Len; |
| 349 |
return Len; |
| 350 |
} |
| 351 |
|
| 352 |
static void skipToNewlineRaw(const char *&First, const char *const End) { |
| 353 |
for (;;) { |
| 354 |
if (First == End) |
| 355 |
return; |
| 356 |
|
| 357 |
unsigned Len = isEOL(First, End); |
| 358 |
if (Len) |
| 359 |
return; |
| 360 |
|
| 361 |
char LastNonWhitespace = ' '; |
| 362 |
do { |
| 363 |
if (!isHorizontalWhitespace(*First)) |
| 364 |
LastNonWhitespace = *First; |
| 365 |
if (++First == End) |
| 366 |
return; |
| 367 |
Len = isEOL(First, End); |
| 368 |
} while (!Len); |
| 369 |
|
| 370 |
if (LastNonWhitespace != '\\') |
| 371 |
return; |
| 372 |
|
| 373 |
First += Len; |
| 374 |
// Keep skipping lines... |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
static void skipLineComment(const char *&First, const char *const End) { |
| 379 |
assert(First[0] == '/' && First[1] == '/'); |
| 380 |
First += 2; |
| 381 |
skipToNewlineRaw(First, End); |
| 382 |
} |
| 383 |
|
| 384 |
static void skipBlockComment(const char *&First, const char *const End) { |
| 385 |
assert(First[0] == '/' && First[1] == '*'); |
| 386 |
if (End - First < 4) { |
| 387 |
First = End; |
| 388 |
return; |
| 389 |
} |
| 390 |
for (First += 3; First != End; ++First) |
| 391 |
if (First[-1] == '*' && First[0] == '/') { |
| 392 |
++First; |
| 393 |
return; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/// \returns True if the current single quotation mark character is a C++14 |
| 398 |
/// digit separator. |
| 399 |
static bool isQuoteCppDigitSeparator(const char *const Start, |
| 400 |
const char *const Cur, |
| 401 |
const char *const End) { |
| 402 |
assert(*Cur == '\'' && "expected quotation character"); |
| 403 |
// skipLine called in places where we don't expect a valid number |
| 404 |
// body before `start` on the same line, so always return false at the start. |
| 405 |
if (Start == Cur) |
| 406 |
return false; |
| 407 |
// The previous character must be a valid PP number character. |
| 408 |
// Make sure that the L, u, U, u8 prefixes don't get marked as a |
| 409 |
// separator though. |
| 410 |
char Prev = *(Cur - 1); |
| 411 |
if (Prev == 'L' || Prev == 'U' || Prev == 'u') |
| 412 |
return false; |
| 413 |
if (Prev == '8' && (Cur - 1 != Start) && *(Cur - 2) == 'u') |
| 414 |
return false; |
| 415 |
if (!isPreprocessingNumberBody(Prev)) |
| 416 |
return false; |
| 417 |
// The next character should be a valid identifier body character. |
| 418 |
return (Cur + 1) < End && isAsciiIdentifierContinue(*(Cur + 1)); |
| 419 |
} |
| 420 |
|
| 421 |
void Scanner::skipLine(const char *&First, const char *const End) { |
| 422 |
for (;;) { |
| 423 |
assert(First <= End); |
| 424 |
if (First == End) |
| 425 |
return; |
| 426 |
|
| 427 |
if (isVerticalWhitespace(*First)) { |
| 428 |
skipNewline(First, End); |
| 429 |
return; |
| 430 |
} |
| 431 |
const char *Start = First; |
| 432 |
// Use `LastNonWhitespace`to track if a line-continuation has ever been seen |
| 433 |
// before a new-line character: |
| 434 |
char LastNonWhitespace = ' '; |
| 435 |
while (First != End && !isVerticalWhitespace(*First)) { |
| 436 |
// Iterate over strings correctly to avoid comments and newlines. |
| 437 |
if (*First == '"' || |
| 438 |
(*First == '\'' && !isQuoteCppDigitSeparator(Start, First, End))) { |
| 439 |
LastTokenPtr = First; |
| 440 |
if (isRawStringLiteral(Start, First)) |
| 441 |
skipRawString(First, End); |
| 442 |
else |
| 443 |
skipString(First, End); |
| 444 |
continue; |
| 445 |
} |
| 446 |
|
| 447 |
// Continue on the same line if an EOL is preceded with backslash |
| 448 |
if (First + 1 < End && *First == '\\') { |
| 449 |
if (unsigned Len = isEOL(First + 1, End)) { |
| 450 |
First += 1 + Len; |
| 451 |
continue; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
// Iterate over comments correctly. |
| 456 |
if (*First != '/' || End - First < 2) { |
| 457 |
LastTokenPtr = First; |
| 458 |
if (!isWhitespace(*First)) |
| 459 |
LastNonWhitespace = *First; |
| 460 |
++First; |
| 461 |
continue; |
| 462 |
} |
| 463 |
|
| 464 |
if (First[1] == '/') { |
| 465 |
// "//...". |
| 466 |
skipLineComment(First, End); |
| 467 |
continue; |
| 468 |
} |
| 469 |
|
| 470 |
if (First[1] != '*') { |
| 471 |
LastTokenPtr = First; |
| 472 |
if (!isWhitespace(*First)) |
| 473 |
LastNonWhitespace = *First; |
| 474 |
++First; |
| 475 |
continue; |
| 476 |
} |
| 477 |
|
| 478 |
// "/*...*/". |
| 479 |
skipBlockComment(First, End); |
| 480 |
} |
| 481 |
if (First == End) |
| 482 |
return; |
| 483 |
|
| 484 |
// Skip over the newline. |
| 485 |
skipNewline(First, End); |
| 486 |
|
| 487 |
if (LastNonWhitespace != '\\') |
| 488 |
break; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
void Scanner::skipDirective(StringRef Name, const char *&First, |
| 493 |
const char *const End) { |
| 494 |
if (llvm::StringSwitch<bool>(Name) |
| 495 |
.Case("warning", true) |
| 496 |
.Case("error", true) |
| 497 |
.Default(false)) |
| 498 |
// Do not process quotes or comments. |
| 499 |
skipToNewlineRaw(First, End); |
| 500 |
else |
| 501 |
skipLine(First, End); |
| 502 |
} |
| 503 |
|
| 504 |
static void skipWhitespace(const char *&First, const char *const End) { |
| 505 |
for (;;) { |
| 506 |
assert(First <= End); |
| 507 |
skipOverSpaces(First, End); |
| 508 |
|
| 509 |
if (End - First < 2) |
| 510 |
return; |
| 511 |
|
| 512 |
if (*First == '\\') { |
| 513 |
const char *Ptr = First + 1; |
| 514 |
while (Ptr < End && isHorizontalWhitespace(*Ptr)) |
| 515 |
++Ptr; |
| 516 |
if (Ptr != End && isVerticalWhitespace(*Ptr)) { |
| 517 |
skipNewline(Ptr, End); |
| 518 |
First = Ptr; |
| 519 |
continue; |
| 520 |
} |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
// Check for a non-comment character. |
| 525 |
if (First[0] != '/') |
| 526 |
return; |
| 527 |
|
| 528 |
// "// ...". |
| 529 |
if (First[1] == '/') { |
| 530 |
skipLineComment(First, End); |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
// Cannot be a comment. |
| 535 |
if (First[1] != '*') |
| 536 |
return; |
| 537 |
|
| 538 |
// "/*...*/". |
| 539 |
skipBlockComment(First, End); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
bool Scanner::lexModuleDirectiveBody(DirectiveKind Kind, const char *&First, |
| 544 |
const char *const End) { |
| 545 |
const char *DirectiveLoc = Input.data() + CurDirToks.front().Offset; |
| 546 |
for (;;) { |
| 547 |
// Keep a copy of the First char incase it needs to be reset. |
| 548 |
const char *Previous = First; |
| 549 |
const dependency_directives_scan::Token &Tok = lexToken(First, End); |
| 550 |
if ((Tok.is(tok::hash) || Tok.is(tok::at)) && |
| 551 |
(Tok.Flags & clang::Token::StartOfLine)) { |
| 552 |
CurDirToks.pop_back(); |
| 553 |
First = Previous; |
| 554 |
return false; |
| 555 |
} |
| 556 |
if (Tok.is(tok::eof)) |
| 557 |
return reportError( |
| 558 |
DirectiveLoc, |
| 559 |
diag::err_dep_source_scanner_missing_semi_after_at_import); |
| 560 |
if (Tok.is(tok::semi)) |
| 561 |
break; |
| 562 |
} |
| 563 |
|
| 564 |
const auto &Tok = lexToken(First, End); |
| 565 |
pushDirective(Kind); |
| 566 |
if (Tok.is(tok::eof) || Tok.is(tok::eod)) |
| 567 |
return false; |
| 568 |
return reportError(DirectiveLoc, |
| 569 |
diag::err_dep_source_scanner_unexpected_tokens_at_import); |
| 570 |
} |
| 571 |
|
| 572 |
dependency_directives_scan::Token &Scanner::lexToken(const char *&First, |
| 573 |
const char *const End) { |
| 574 |
clang::Token Tok; |
| 575 |
TheLexer.LexFromRawLexer(Tok); |
| 576 |
First = Input.data() + TheLexer.getCurrentBufferOffset(); |
| 577 |
assert(First <= End); |
| 578 |
|
| 579 |
unsigned Offset = TheLexer.getCurrentBufferOffset() - Tok.getLength(); |
| 580 |
CurDirToks.emplace_back(Offset, Tok.getLength(), Tok.getKind(), |
| 581 |
Tok.getFlags()); |
| 582 |
return CurDirToks.back(); |
| 583 |
} |
| 584 |
|
| 585 |
dependency_directives_scan::Token & |
| 586 |
Scanner::lexIncludeFilename(const char *&First, const char *const End) { |
| 587 |
clang::Token Tok; |
| 588 |
TheLexer.LexIncludeFilename(Tok); |
| 589 |
First = Input.data() + TheLexer.getCurrentBufferOffset(); |
| 590 |
assert(First <= End); |
| 591 |
|
| 592 |
unsigned Offset = TheLexer.getCurrentBufferOffset() - Tok.getLength(); |
| 593 |
CurDirToks.emplace_back(Offset, Tok.getLength(), Tok.getKind(), |
| 594 |
Tok.getFlags()); |
| 595 |
return CurDirToks.back(); |
| 596 |
} |
| 597 |
|
| 598 |
void Scanner::lexPPDirectiveBody(const char *&First, const char *const End) { |
| 599 |
while (true) { |
| 600 |
const dependency_directives_scan::Token &Tok = lexToken(First, End); |
| 601 |
if (Tok.is(tok::eod) || Tok.is(tok::eof)) |
| 602 |
break; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
StringRef |
| 607 |
Scanner::cleanStringIfNeeded(const dependency_directives_scan::Token &Tok) { |
| 608 |
bool NeedsCleaning = Tok.Flags & clang::Token::NeedsCleaning; |
| 609 |
if (LLVM_LIKELY(!NeedsCleaning)) |
| 610 |
return Input.slice(Tok.Offset, Tok.getEnd()); |
| 611 |
|
| 612 |
SmallString<64> Spelling; |
| 613 |
Spelling.resize(Tok.Length); |
| 614 |
|
| 615 |
// FIXME: C++11 raw string literals need special handling (see getSpellingSlow |
| 616 |
// in the Lexer). Currently we cannot see them due to our LangOpts. |
| 617 |
|
| 618 |
unsigned SpellingLength = 0; |
| 619 |
const char *BufPtr = Input.begin() + Tok.Offset; |
| 620 |
const char *AfterIdent = Input.begin() + Tok.getEnd(); |
| 621 |
while (BufPtr < AfterIdent) { |
| 622 |
auto [Char, Size] = Lexer::getCharAndSizeNoWarn(BufPtr, LangOpts); |
| 623 |
Spelling[SpellingLength++] = Char; |
| 624 |
BufPtr += Size; |
| 625 |
} |
| 626 |
|
| 627 |
return SplitIds.try_emplace(StringRef(Spelling.begin(), SpellingLength), 0) |
| 628 |
.first->first(); |
| 629 |
} |
| 630 |
|
| 631 |
std::optional<StringRef> |
| 632 |
Scanner::tryLexIdentifierOrSkipLine(const char *&First, const char *const End) { |
| 633 |
const dependency_directives_scan::Token &Tok = lexToken(First, End); |
| 634 |
if (Tok.isNot(tok::raw_identifier)) { |
| 635 |
if (!Tok.is(tok::eod)) |
| 636 |
skipLine(First, End); |
| 637 |
return std::nullopt; |
| 638 |
} |
| 639 |
|
| 640 |
return cleanStringIfNeeded(Tok); |
| 641 |
} |
| 642 |
|
| 643 |
StringRef Scanner::lexIdentifier(const char *&First, const char *const End) { |
| 644 |
std::optional<StringRef> Id = tryLexIdentifierOrSkipLine(First, End); |
| 645 |
assert(Id && "expected identifier token"); |
| 646 |
return *Id; |
| 647 |
} |
| 648 |
|
| 649 |
bool Scanner::isNextIdentifierOrSkipLine(StringRef Id, const char *&First, |
| 650 |
const char *const End) { |
| 651 |
if (std::optional<StringRef> FoundId = |
| 652 |
tryLexIdentifierOrSkipLine(First, End)) { |
| 653 |
if (*FoundId == Id) |
| 654 |
return true; |
| 655 |
skipLine(First, End); |
| 656 |
} |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
bool Scanner::isNextTokenOrSkipLine(tok::TokenKind K, const char *&First, |
| 661 |
const char *const End) { |
| 662 |
const dependency_directives_scan::Token &Tok = lexToken(First, End); |
| 663 |
if (Tok.is(K)) |
| 664 |
return true; |
| 665 |
skipLine(First, End); |
| 666 |
return false; |
| 667 |
} |
| 668 |
|
| 669 |
std::optional<StringRef> |
| 670 |
Scanner::tryLexStringLiteralOrSkipLine(const char *&First, |
| 671 |
const char *const End) { |
| 672 |
const dependency_directives_scan::Token &Tok = lexToken(First, End); |
| 673 |
if (!tok::isStringLiteral(Tok.Kind)) { |
| 674 |
if (!Tok.is(tok::eod)) |
| 675 |
skipLine(First, End); |
| 676 |
return std::nullopt; |
| 677 |
} |
| 678 |
|
| 679 |
return cleanStringIfNeeded(Tok); |
| 680 |
} |
| 681 |
|
| 682 |
bool Scanner::lexAt(const char *&First, const char *const End) { |
| 683 |
// Handle "@import". |
| 684 |
|
| 685 |
// Lex '@'. |
| 686 |
const dependency_directives_scan::Token &AtTok = lexToken(First, End); |
| 687 |
assert(AtTok.is(tok::at)); |
| 688 |
(void)AtTok; |
| 689 |
|
| 690 |
if (!isNextIdentifierOrSkipLine("import", First, End)) |
| 691 |
return false; |
| 692 |
return lexModuleDirectiveBody(decl_at_import, First, End); |
| 693 |
} |
| 694 |
|
| 695 |
bool Scanner::lexModule(const char *&First, const char *const End) { |
| 696 |
StringRef Id = lexIdentifier(First, End); |
| 697 |
bool Export = false; |
| 698 |
if (Id == "export") { |
| 699 |
Export = true; |
| 700 |
std::optional<StringRef> NextId = tryLexIdentifierOrSkipLine(First, End); |
| 701 |
if (!NextId) |
| 702 |
return false; |
| 703 |
Id = *NextId; |
| 704 |
} |
| 705 |
|
| 706 |
if (Id != "module" && Id != "import") { |
| 707 |
skipLine(First, End); |
| 708 |
return false; |
| 709 |
} |
| 710 |
|
| 711 |
skipWhitespace(First, End); |
| 712 |
|
| 713 |
// Ignore this as a module directive if the next character can't be part of |
| 714 |
// an import. |
| 715 |
|
| 716 |
switch (*First) { |
| 717 |
case ':': { |
| 718 |
// `module :` is never the start of a valid module declaration. |
| 719 |
if (Id == "module") { |
| 720 |
skipLine(First, End); |
| 721 |
return false; |
| 722 |
} |
| 723 |
// A module partition starts with exactly one ':'. If we have '::', this is |
| 724 |
// a scope resolution instead and shouldn't be recognized as a directive |
| 725 |
// per P1857R3. |
| 726 |
if (First + 1 != End && First[1] == ':') { |
| 727 |
skipLine(First, End); |
| 728 |
return false; |
| 729 |
} |
| 730 |
// `import:(type)name` is a valid ObjC method decl, so check one more token. |
| 731 |
(void)lexToken(First, End); |
| 732 |
if (!tryLexIdentifierOrSkipLine(First, End)) |
| 733 |
return false; |
| 734 |
break; |
| 735 |
} |
| 736 |
case ';': { |
| 737 |
// Handle the global module fragment `module;`. |
| 738 |
if (Id == "module" && !Export) |
| 739 |
break; |
| 740 |
skipLine(First, End); |
| 741 |
return false; |
| 742 |
} |
| 743 |
case '<': |
| 744 |
case '"': |
| 745 |
break; |
| 746 |
default: |
| 747 |
if (!isAsciiIdentifierContinue(*First)) { |
| 748 |
skipLine(First, End); |
| 749 |
return false; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
TheLexer.seek(getOffsetAt(First), /*IsAtStartOfLine*/ false); |
| 754 |
|
| 755 |
DirectiveKind Kind; |
| 756 |
if (Id == "module") |
| 757 |
Kind = Export ? cxx_export_module_decl : cxx_module_decl; |
| 758 |
else |
| 759 |
Kind = Export ? cxx_export_import_decl : cxx_import_decl; |
| 760 |
|
| 761 |
return lexModuleDirectiveBody(Kind, First, End); |
| 762 |
} |
| 763 |
|
| 764 |
bool Scanner::lex_Pragma(const char *&First, const char *const End) { |
| 765 |
if (!isNextTokenOrSkipLine(tok::l_paren, First, End)) |
| 766 |
return false; |
| 767 |
|
| 768 |
std::optional<StringRef> Str = tryLexStringLiteralOrSkipLine(First, End); |
| 769 |
|
| 770 |
if (!Str || !isNextTokenOrSkipLine(tok::r_paren, First, End)) |
| 771 |
return false; |
| 772 |
|
| 773 |
SmallString<64> Buffer(*Str); |
| 774 |
prepare_PragmaString(Buffer); |
| 775 |
|
| 776 |
// Use a new scanner instance since the tokens will be inside the allocated |
| 777 |
// string. We should already have captured all the relevant tokens in the |
| 778 |
// current scanner. |
| 779 |
SmallVector<dependency_directives_scan::Token> DiscardTokens; |
| 780 |
const char *Begin = Buffer.c_str(); |
| 781 |
Scanner PragmaScanner{StringRef(Begin, Buffer.size()), DiscardTokens, Diags, |
| 782 |
InputSourceLoc}; |
| 783 |
|
| 784 |
PragmaScanner.TheLexer.setParsingPreprocessorDirective(true); |
| 785 |
if (PragmaScanner.lexPragma(Begin, Buffer.end())) |
| 786 |
return true; |
| 787 |
|
| 788 |
DirectiveKind K = PragmaScanner.topDirective(); |
| 789 |
if (K == pp_none) { |
| 790 |
skipLine(First, End); |
| 791 |
return false; |
| 792 |
} |
| 793 |
|
| 794 |
assert(Begin == Buffer.end()); |
| 795 |
pushDirective(K); |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
bool Scanner::lexPragma(const char *&First, const char *const End) { |
| 800 |
std::optional<StringRef> FoundId = tryLexIdentifierOrSkipLine(First, End); |
| 801 |
if (!FoundId) |
| 802 |
return false; |
| 803 |
|
| 804 |
StringRef Id = *FoundId; |
| 805 |
auto Kind = llvm::StringSwitch<DirectiveKind>(Id) |
| 806 |
.Case("once", pp_pragma_once) |
| 807 |
.Case("push_macro", pp_pragma_push_macro) |
| 808 |
.Case("pop_macro", pp_pragma_pop_macro) |
| 809 |
.Case("include_alias", pp_pragma_include_alias) |
| 810 |
.Default(pp_none); |
| 811 |
if (Kind != pp_none) { |
| 812 |
lexPPDirectiveBody(First, End); |
| 813 |
pushDirective(Kind); |
| 814 |
return false; |
| 815 |
} |
| 816 |
|
| 817 |
if (Id != "clang") { |
| 818 |
skipLine(First, End); |
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
FoundId = tryLexIdentifierOrSkipLine(First, End); |
| 823 |
if (!FoundId) |
| 824 |
return false; |
| 825 |
Id = *FoundId; |
| 826 |
|
| 827 |
// #pragma clang system_header |
| 828 |
if (Id == "system_header") { |
| 829 |
lexPPDirectiveBody(First, End); |
| 830 |
pushDirective(pp_pragma_system_header); |
| 831 |
return false; |
| 832 |
} |
| 833 |
|
| 834 |
if (Id != "module") { |
| 835 |
skipLine(First, End); |
| 836 |
return false; |
| 837 |
} |
| 838 |
|
| 839 |
// #pragma clang module. |
| 840 |
if (!isNextIdentifierOrSkipLine("import", First, End)) |
| 841 |
return false; |
| 842 |
|
| 843 |
// #pragma clang module import. |
| 844 |
lexPPDirectiveBody(First, End); |
| 845 |
pushDirective(pp_pragma_import); |
| 846 |
return false; |
| 847 |
} |
| 848 |
|
| 849 |
bool Scanner::lexEndif(const char *&First, const char *const End) { |
| 850 |
// Strip out "#else" if it's empty. |
| 851 |
if (topDirective() == pp_else) |
| 852 |
popDirective(); |
| 853 |
|
| 854 |
// If "#ifdef" is empty, strip it and skip the "#endif". |
| 855 |
// |
| 856 |
// FIXME: Once/if Clang starts disallowing __has_include in macro expansions, |
| 857 |
// we can skip empty `#if` and `#elif` blocks as well after scanning for a |
| 858 |
// literal __has_include in the condition. Even without that rule we could |
| 859 |
// drop the tokens if we scan for identifiers in the condition and find none. |
| 860 |
if (topDirective() == pp_ifdef || topDirective() == pp_ifndef) { |
| 861 |
popDirective(); |
| 862 |
skipLine(First, End); |
| 863 |
return false; |
| 864 |
} |
| 865 |
|
| 866 |
return lexDefault(pp_endif, First, End); |
| 867 |
} |
| 868 |
|
| 869 |
bool Scanner::lexDefault(DirectiveKind Kind, const char *&First, |
| 870 |
const char *const End) { |
| 871 |
lexPPDirectiveBody(First, End); |
| 872 |
pushDirective(Kind); |
| 873 |
return false; |
| 874 |
} |
| 875 |
|
| 876 |
static bool isStartOfRelevantLine(char First) { |
| 877 |
switch (First) { |
| 878 |
case '#': |
| 879 |
case '@': |
| 880 |
case 'i': |
| 881 |
case 'e': |
| 882 |
case 'm': |
| 883 |
case '_': |
| 884 |
return true; |
| 885 |
} |
| 886 |
return false; |
| 887 |
} |
| 888 |
|
| 889 |
bool Scanner::lexPPLine(const char *&First, const char *const End) { |
| 890 |
assert(First != End); |
| 891 |
|
| 892 |
skipWhitespace(First, End); |
| 893 |
assert(First <= End); |
| 894 |
if (First == End) |
| 895 |
return false; |
| 896 |
|
| 897 |
if (!isStartOfRelevantLine(*First)) { |
| 898 |
skipLine(First, End); |
| 899 |
assert(First <= End); |
| 900 |
return false; |
| 901 |
} |
| 902 |
|
| 903 |
LastTokenPtr = First; |
| 904 |
|
| 905 |
TheLexer.seek(getOffsetAt(First), /*IsAtStartOfLine*/ true); |
| 906 |
|
| 907 |
llvm::scope_exit ScEx1([&]() { |
| 908 |
/// Clear Scanner's CurDirToks before returning, in case we didn't push a |
| 909 |
/// new directive. |
| 910 |
CurDirToks.clear(); |
| 911 |
}); |
| 912 |
|
| 913 |
if (*First == '_') { |
| 914 |
if (isNextIdentifierOrSkipLine("_Pragma", First, End)) |
| 915 |
return lex_Pragma(First, End); |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
// Handle preprocessing directives. |
| 920 |
|
| 921 |
TheLexer.setParsingPreprocessorDirective(true); |
| 922 |
llvm::scope_exit ScEx2( |
| 923 |
[&]() { TheLexer.setParsingPreprocessorDirective(false); }); |
| 924 |
|
| 925 |
// Handle "@import". |
| 926 |
if (*First == '@') |
| 927 |
return lexAt(First, End); |
| 928 |
|
| 929 |
// Handle module directives for C++20 modules. |
| 930 |
if (*First == 'i' || *First == 'e' || *First == 'm') |
| 931 |
return lexModule(First, End); |
| 932 |
|
| 933 |
// Lex '#'. |
| 934 |
const dependency_directives_scan::Token &HashTok = lexToken(First, End); |
| 935 |
if (HashTok.is(tok::hashhash)) { |
| 936 |
// A \p tok::hashhash at this location is passed by the preprocessor to the |
| 937 |
// parser to interpret, like any other token. So for dependency scanning |
| 938 |
// skip it like a normal token not affecting the preprocessor. |
| 939 |
skipLine(First, End); |
| 940 |
assert(First <= End); |
| 941 |
return false; |
| 942 |
} |
| 943 |
assert(HashTok.is(tok::hash)); |
| 944 |
(void)HashTok; |
| 945 |
|
| 946 |
std::optional<StringRef> FoundId = tryLexIdentifierOrSkipLine(First, End); |
| 947 |
if (!FoundId) |
| 948 |
return false; |
| 949 |
|
| 950 |
StringRef Id = *FoundId; |
| 951 |
|
| 952 |
if (Id == "pragma") |
| 953 |
return lexPragma(First, End); |
| 954 |
|
| 955 |
auto Kind = llvm::StringSwitch<DirectiveKind>(Id) |
| 956 |
.Case("include", pp_include) |
| 957 |
.Case("__include_macros", pp___include_macros) |
| 958 |
.Case("define", pp_define) |
| 959 |
.Case("undef", pp_undef) |
| 960 |
.Case("import", pp_import) |
| 961 |
.Case("include_next", pp_include_next) |
| 962 |
.Case("if", pp_if) |
| 963 |
.Case("ifdef", pp_ifdef) |
| 964 |
.Case("ifndef", pp_ifndef) |
| 965 |
.Case("elif", pp_elif) |
| 966 |
.Case("elifdef", pp_elifdef) |
| 967 |
.Case("elifndef", pp_elifndef) |
| 968 |
.Case("else", pp_else) |
| 969 |
.Case("endif", pp_endif) |
| 970 |
.Default(pp_none); |
| 971 |
if (Kind == pp_none) { |
| 972 |
skipDirective(Id, First, End); |
| 973 |
return false; |
| 974 |
} |
| 975 |
|
| 976 |
if (Kind == pp_endif) |
| 977 |
return lexEndif(First, End); |
| 978 |
|
| 979 |
switch (Kind) { |
| 980 |
case pp_include: |
| 981 |
case pp___include_macros: |
| 982 |
case pp_include_next: |
| 983 |
case pp_import: |
| 984 |
// Ignore missing filenames in include or import directives. |
| 985 |
if (lexIncludeFilename(First, End).is(tok::eod)) { |
| 986 |
return false; |
| 987 |
} |
| 988 |
break; |
| 989 |
default: |
| 990 |
break; |
| 991 |
} |
| 992 |
|
| 993 |
// Everything else. |
| 994 |
return lexDefault(Kind, First, End); |
| 995 |
} |
| 996 |
|
| 997 |
static void skipUTF8ByteOrderMark(const char *&First, const char *const End) { |
| 998 |
if ((End - First) >= 3 && First[0] == '\xef' && First[1] == '\xbb' && |
| 999 |
First[2] == '\xbf') |
| 1000 |
First += 3; |
| 1001 |
} |
| 1002 |
|
| 1003 |
bool Scanner::scanImpl(const char *First, const char *const End) { |
| 1004 |
skipUTF8ByteOrderMark(First, End); |
| 1005 |
while (First != End) |
| 1006 |
if (lexPPLine(First, End)) |
| 1007 |
return true; |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
|
| 1011 |
bool Scanner::scan(SmallVectorImpl<Directive> &Directives) { |
| 1012 |
bool Error = scanImpl(Input.begin(), Input.end()); |
| 1013 |
|
| 1014 |
if (!Error) { |
| 1015 |
// Add an EOF on success. |
| 1016 |
if (LastTokenPtr && |
| 1017 |
(Tokens.empty() || LastTokenPtr > Input.begin() + Tokens.back().Offset)) |
| 1018 |
pushDirective(tokens_present_before_eof); |
| 1019 |
pushDirective(pp_eof); |
| 1020 |
} |
| 1021 |
|
| 1022 |
ArrayRef<dependency_directives_scan::Token> RemainingTokens = Tokens; |
| 1023 |
for (const DirectiveWithTokens &DirWithToks : DirsWithToks) { |
| 1024 |
assert(RemainingTokens.size() >= DirWithToks.NumTokens); |
| 1025 |
Directives.emplace_back(DirWithToks.Kind, |
| 1026 |
RemainingTokens.take_front(DirWithToks.NumTokens)); |
| 1027 |
RemainingTokens = RemainingTokens.drop_front(DirWithToks.NumTokens); |
| 1028 |
} |
| 1029 |
assert(RemainingTokens.empty()); |
| 1030 |
|
| 1031 |
return Error; |
| 1032 |
} |
| 1033 |
|
| 1034 |
bool clang::scanSourceForDependencyDirectives( |
| 1035 |
StringRef Input, SmallVectorImpl<dependency_directives_scan::Token> &Tokens, |
| 1036 |
SmallVectorImpl<Directive> &Directives, DiagnosticsEngine *Diags, |
| 1037 |
SourceLocation InputSourceLoc) { |
| 1038 |
return Scanner(Input, Tokens, Diags, InputSourceLoc).scan(Directives); |
| 1039 |
} |
| 1040 |
|
| 1041 |
void clang::printDependencyDirectivesAsSource( |
| 1042 |
StringRef Source, |
| 1043 |
ArrayRef<dependency_directives_scan::Directive> Directives, |
| 1044 |
llvm::raw_ostream &OS) { |
| 1045 |
// Add a space separator where it is convenient for testing purposes. |
| 1046 |
auto needsSpaceSeparator = |
| 1047 |
[](tok::TokenKind Prev, |
| 1048 |
const dependency_directives_scan::Token &Tok) -> bool { |
| 1049 |
if (Prev == Tok.Kind) |
| 1050 |
return !Tok.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, |
| 1051 |
tok::r_square); |
| 1052 |
if (Prev == tok::raw_identifier && |
| 1053 |
Tok.isOneOf(tok::hash, tok::numeric_constant, tok::string_literal, |
| 1054 |
tok::char_constant, tok::header_name)) |
| 1055 |
return true; |
| 1056 |
if (Prev == tok::r_paren && |
| 1057 |
Tok.isOneOf(tok::raw_identifier, tok::hash, tok::string_literal, |
| 1058 |
tok::char_constant, tok::unknown)) |
| 1059 |
return true; |
| 1060 |
if (Prev == tok::comma && |
| 1061 |
Tok.isOneOf(tok::l_paren, tok::string_literal, tok::less)) |
| 1062 |
return true; |
| 1063 |
return false; |
| 1064 |
}; |
| 1065 |
|
| 1066 |
for (const dependency_directives_scan::Directive &Directive : Directives) { |
| 1067 |
if (Directive.Kind == tokens_present_before_eof) |
| 1068 |
OS << "<TokBeforeEOF>"; |
| 1069 |
std::optional<tok::TokenKind> PrevTokenKind; |
| 1070 |
for (const dependency_directives_scan::Token &Tok : Directive.Tokens) { |
| 1071 |
if (PrevTokenKind && needsSpaceSeparator(*PrevTokenKind, Tok)) |
| 1072 |
OS << ' '; |
| 1073 |
PrevTokenKind = Tok.Kind; |
| 1074 |
OS << Source.slice(Tok.Offset, Tok.getEnd()); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
} |