1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/Rewrite/Frontend/Rewriters.h" |
15 | #include "clang/Basic/SourceManager.h" |
16 | #include "clang/Frontend/PreprocessorOutputOptions.h" |
17 | #include "clang/Lex/HeaderSearch.h" |
18 | #include "clang/Lex/Pragma.h" |
19 | #include "clang/Lex/Preprocessor.h" |
20 | #include "llvm/ADT/SmallString.h" |
21 | #include "llvm/Support/raw_ostream.h" |
22 | |
23 | using namespace clang; |
24 | using namespace llvm; |
25 | |
26 | namespace { |
27 | |
28 | class InclusionRewriter : public PPCallbacks { |
29 | |
30 | |
31 | struct IncludedFile { |
32 | FileID Id; |
33 | SrcMgr::CharacteristicKind FileType; |
34 | const DirectoryLookup *DirLookup; |
35 | IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType, |
36 | const DirectoryLookup *DirLookup) |
37 | : Id(Id), FileType(FileType), DirLookup(DirLookup) {} |
38 | }; |
39 | Preprocessor &PP; |
40 | SourceManager &SM; |
41 | raw_ostream &OS; |
42 | StringRef MainEOL; |
43 | const llvm::MemoryBuffer *PredefinesBuffer; |
44 | bool ShowLineMarkers; |
45 | bool UseLineDirectives; |
46 | |
47 | std::map<unsigned, IncludedFile> FileIncludes; |
48 | |
49 | std::map<unsigned, const Module *> ModuleIncludes; |
50 | |
51 | std::map<unsigned, const Module *> ModuleEntryIncludes; |
52 | |
53 | |
54 | SourceLocation LastInclusionLocation; |
55 | public: |
56 | InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers, |
57 | bool UseLineDirectives); |
58 | void Process(FileID FileId, SrcMgr::CharacteristicKind FileType, |
59 | const DirectoryLookup *DirLookup); |
60 | void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) { |
61 | PredefinesBuffer = Buf; |
62 | } |
63 | void detectMainFileEOL(); |
64 | void handleModuleBegin(Token &Tok) { |
65 | assert(Tok.getKind() == tok::annot_module_begin); |
66 | ModuleEntryIncludes.insert({Tok.getLocation().getRawEncoding(), |
67 | (Module *)Tok.getAnnotationValue()}); |
68 | } |
69 | private: |
70 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
71 | SrcMgr::CharacteristicKind FileType, |
72 | FileID PrevFID) override; |
73 | void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok, |
74 | SrcMgr::CharacteristicKind FileType) override; |
75 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
76 | StringRef FileName, bool IsAngled, |
77 | CharSourceRange FilenameRange, const FileEntry *File, |
78 | StringRef SearchPath, StringRef RelativePath, |
79 | const Module *Imported, |
80 | SrcMgr::CharacteristicKind FileType) override; |
81 | void WriteLineInfo(StringRef Filename, int Line, |
82 | SrcMgr::CharacteristicKind FileType, |
83 | StringRef = StringRef()); |
84 | void WriteImplicitModuleImport(const Module *Mod); |
85 | void OutputContentUpTo(const MemoryBuffer &FromFile, |
86 | unsigned &WriteFrom, unsigned WriteTo, |
87 | StringRef EOL, int &lines, |
88 | bool EnsureNewline); |
89 | void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken, |
90 | const MemoryBuffer &FromFile, StringRef EOL, |
91 | unsigned &NextToWrite, int &Lines); |
92 | bool HandleHasInclude(FileID FileId, Lexer &RawLex, |
93 | const DirectoryLookup *Lookup, Token &Tok, |
94 | bool &FileExists); |
95 | const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const; |
96 | const Module *FindModuleAtLocation(SourceLocation Loc) const; |
97 | const Module *FindEnteredModule(SourceLocation Loc) const; |
98 | StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken); |
99 | }; |
100 | |
101 | } |
102 | |
103 | |
104 | InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS, |
105 | bool ShowLineMarkers, |
106 | bool UseLineDirectives) |
107 | : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"), |
108 | PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers), |
109 | UseLineDirectives(UseLineDirectives), |
110 | LastInclusionLocation(SourceLocation()) {} |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line, |
117 | SrcMgr::CharacteristicKind FileType, |
118 | StringRef ) { |
119 | if (!ShowLineMarkers) |
120 | return; |
121 | if (UseLineDirectives) { |
122 | OS << "#line" << ' ' << Line << ' ' << '"'; |
123 | OS.write_escaped(Filename); |
124 | OS << '"'; |
125 | } else { |
126 | |
127 | |
128 | OS << '#' << ' ' << Line << ' ' << '"'; |
129 | OS.write_escaped(Filename); |
130 | OS << '"'; |
131 | if (!Extra.empty()) |
132 | OS << Extra; |
133 | if (FileType == SrcMgr::C_System) |
134 | |
135 | |
136 | OS << " 3"; |
137 | else if (FileType == SrcMgr::C_ExternCSystem) |
138 | |
139 | |
140 | OS << " 3 4"; |
141 | } |
142 | OS << MainEOL; |
143 | } |
144 | |
145 | void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) { |
146 | OS << "#pragma clang module import " << Mod->getFullModuleName(true) |
147 | << " /* clang -frewrite-includes: implicit import */" << MainEOL; |
148 | } |
149 | |
150 | |
151 | |
152 | void InclusionRewriter::FileChanged(SourceLocation Loc, |
153 | FileChangeReason Reason, |
154 | SrcMgr::CharacteristicKind NewFileType, |
155 | FileID) { |
156 | if (Reason != EnterFile) |
157 | return; |
158 | if (LastInclusionLocation.isInvalid()) |
159 | |
160 | return; |
161 | FileID Id = FullSourceLoc(Loc, SM).getFileID(); |
162 | auto P = FileIncludes.insert( |
163 | std::make_pair(LastInclusionLocation.getRawEncoding(), |
164 | IncludedFile(Id, NewFileType, PP.GetCurDirLookup()))); |
165 | (void)P; |
166 | (0) . __assert_fail ("P.second && \"Unexpected revisitation of the same include directive\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp", 166, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(P.second && "Unexpected revisitation of the same include directive"); |
167 | LastInclusionLocation = SourceLocation(); |
168 | } |
169 | |
170 | |
171 | |
172 | void InclusionRewriter::FileSkipped(const FileEntry &, |
173 | const Token &, |
174 | SrcMgr::CharacteristicKind ) { |
175 | (0) . __assert_fail ("LastInclusionLocation.isValid() && \"A file, that wasn't found via an inclusion directive, was skipped\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp", 176, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(LastInclusionLocation.isValid() && |
176 | (0) . __assert_fail ("LastInclusionLocation.isValid() && \"A file, that wasn't found via an inclusion directive, was skipped\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp", 176, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "A file, that wasn't found via an inclusion directive, was skipped"); |
177 | LastInclusionLocation = SourceLocation(); |
178 | } |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | void InclusionRewriter::InclusionDirective(SourceLocation HashLoc, |
188 | const Token &, |
189 | StringRef , |
190 | bool , |
191 | CharSourceRange , |
192 | const FileEntry * , |
193 | StringRef , |
194 | StringRef , |
195 | const Module *Imported, |
196 | SrcMgr::CharacteristicKind FileType){ |
197 | if (Imported) { |
198 | auto P = ModuleIncludes.insert( |
199 | std::make_pair(HashLoc.getRawEncoding(), Imported)); |
200 | (void)P; |
201 | (0) . __assert_fail ("P.second && \"Unexpected revisitation of the same include directive\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp", 201, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(P.second && "Unexpected revisitation of the same include directive"); |
202 | } else |
203 | LastInclusionLocation = HashLoc; |
204 | } |
205 | |
206 | |
207 | |
208 | const InclusionRewriter::IncludedFile * |
209 | InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const { |
210 | const auto I = FileIncludes.find(Loc.getRawEncoding()); |
211 | if (I != FileIncludes.end()) |
212 | return &I->second; |
213 | return nullptr; |
214 | } |
215 | |
216 | |
217 | |
218 | const Module * |
219 | InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const { |
220 | const auto I = ModuleIncludes.find(Loc.getRawEncoding()); |
221 | if (I != ModuleIncludes.end()) |
222 | return I->second; |
223 | return nullptr; |
224 | } |
225 | |
226 | |
227 | |
228 | const Module * |
229 | InclusionRewriter::FindEnteredModule(SourceLocation Loc) const { |
230 | const auto I = ModuleEntryIncludes.find(Loc.getRawEncoding()); |
231 | if (I != ModuleEntryIncludes.end()) |
232 | return I->second; |
233 | return nullptr; |
234 | } |
235 | |
236 | |
237 | |
238 | static StringRef DetectEOL(const MemoryBuffer &FromFile) { |
239 | |
240 | |
241 | |
242 | const char *Pos = strchr(FromFile.getBufferStart(), '\n'); |
243 | if (!Pos) |
244 | return "\n"; |
245 | if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r') |
246 | return "\r\n"; |
247 | if (Pos + 1 < FromFile.getBufferEnd() && Pos[1] == '\r') |
248 | return "\n\r"; |
249 | return "\n"; |
250 | } |
251 | |
252 | void InclusionRewriter::detectMainFileEOL() { |
253 | bool Invalid; |
254 | const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid); |
255 | assert(!Invalid); |
256 | if (Invalid) |
257 | return; |
258 | MainEOL = DetectEOL(FromFile); |
259 | } |
260 | |
261 | |
262 | |
263 | void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile, |
264 | unsigned &WriteFrom, unsigned WriteTo, |
265 | StringRef LocalEOL, int &Line, |
266 | bool EnsureNewline) { |
267 | if (WriteTo <= WriteFrom) |
268 | return; |
269 | if (&FromFile == PredefinesBuffer) { |
270 | |
271 | WriteFrom = WriteTo; |
272 | return; |
273 | } |
274 | |
275 | |
276 | |
277 | |
278 | if (LocalEOL.size() == 2 && |
279 | LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] && |
280 | LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0]) |
281 | WriteTo++; |
282 | |
283 | StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom, |
284 | WriteTo - WriteFrom); |
285 | |
286 | if (MainEOL == LocalEOL) { |
287 | OS << TextToWrite; |
288 | |
289 | Line += TextToWrite.count(LocalEOL); |
290 | if (EnsureNewline && !TextToWrite.endswith(LocalEOL)) |
291 | OS << MainEOL; |
292 | } else { |
293 | |
294 | StringRef Rest = TextToWrite; |
295 | while (!Rest.empty()) { |
296 | StringRef LineText; |
297 | std::tie(LineText, Rest) = Rest.split(LocalEOL); |
298 | OS << LineText; |
299 | Line++; |
300 | if (!Rest.empty()) |
301 | OS << MainEOL; |
302 | } |
303 | if (TextToWrite.endswith(LocalEOL) || EnsureNewline) |
304 | OS << MainEOL; |
305 | } |
306 | WriteFrom = WriteTo; |
307 | } |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | void InclusionRewriter::(Lexer &DirectiveLex, |
315 | const Token &StartToken, |
316 | const MemoryBuffer &FromFile, |
317 | StringRef LocalEOL, |
318 | unsigned &NextToWrite, int &Line) { |
319 | OutputContentUpTo(FromFile, NextToWrite, |
320 | SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, |
321 | false); |
322 | Token DirectiveToken; |
323 | do { |
324 | DirectiveLex.LexFromRawLexer(DirectiveToken); |
325 | } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof)); |
326 | if (&FromFile == PredefinesBuffer) { |
327 | |
328 | return; |
329 | } |
330 | OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; |
331 | OutputContentUpTo(FromFile, NextToWrite, |
332 | SM.getFileOffset(DirectiveToken.getLocation()) + |
333 | DirectiveToken.getLength(), |
334 | LocalEOL, Line, true); |
335 | OS << "#endif /* expanded by -frewrite-includes */" << MainEOL; |
336 | } |
337 | |
338 | |
339 | StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex, |
340 | Token &RawToken) { |
341 | RawLex.LexFromRawLexer(RawToken); |
342 | if (RawToken.is(tok::raw_identifier)) |
343 | PP.LookUpIdentifierInfo(RawToken); |
344 | if (RawToken.is(tok::identifier)) |
345 | return RawToken.getIdentifierInfo()->getName(); |
346 | return StringRef(); |
347 | } |
348 | |
349 | |
350 | |
351 | bool InclusionRewriter::HandleHasInclude( |
352 | FileID FileId, Lexer &RawLex, const DirectoryLookup *Lookup, Token &Tok, |
353 | bool &FileExists) { |
354 | |
355 | RawLex.LexFromRawLexer(Tok); |
356 | if (Tok.isNot(tok::l_paren)) |
357 | return false; |
358 | |
359 | RawLex.LexFromRawLexer(Tok); |
360 | |
361 | SmallString<128> FilenameBuffer; |
362 | StringRef Filename; |
363 | |
364 | |
365 | |
366 | if (Tok.is(tok::less)) { |
367 | RawLex.LexFromRawLexer(Tok); |
368 | |
369 | FilenameBuffer += '<'; |
370 | do { |
371 | if (Tok.is(tok::eod)) |
372 | return false; |
373 | |
374 | if (Tok.is(tok::raw_identifier)) |
375 | PP.LookUpIdentifierInfo(Tok); |
376 | |
377 | |
378 | SmallVector<char, 128> TmpBuffer; |
379 | bool Invalid = false; |
380 | StringRef TmpName = PP.getSpelling(Tok, TmpBuffer, &Invalid); |
381 | if (Invalid) |
382 | return false; |
383 | |
384 | FilenameBuffer += TmpName; |
385 | |
386 | RawLex.LexFromRawLexer(Tok); |
387 | } while (Tok.isNot(tok::greater)); |
388 | |
389 | FilenameBuffer += '>'; |
390 | Filename = FilenameBuffer; |
391 | } else { |
392 | if (Tok.isNot(tok::string_literal)) |
393 | return false; |
394 | |
395 | bool Invalid = false; |
396 | Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); |
397 | if (Invalid) |
398 | return false; |
399 | } |
400 | |
401 | |
402 | RawLex.LexFromRawLexer(Tok); |
403 | if (Tok.isNot(tok::r_paren)) |
404 | return false; |
405 | |
406 | |
407 | |
408 | bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); |
409 | const DirectoryLookup *CurDir; |
410 | const FileEntry *FileEnt = PP.getSourceManager().getFileEntryForID(FileId); |
411 | SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> |
412 | Includers; |
413 | Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); |
414 | |
415 | const FileEntry *File = PP.getHeaderSearchInfo().LookupFile( |
416 | Filename, SourceLocation(), isAngled, Lookup, CurDir, Includers, nullptr, |
417 | nullptr, nullptr, nullptr, nullptr, nullptr); |
418 | |
419 | FileExists = File != nullptr; |
420 | return true; |
421 | } |
422 | |
423 | |
424 | |
425 | void InclusionRewriter::Process(FileID FileId, |
426 | SrcMgr::CharacteristicKind FileType, |
427 | const DirectoryLookup *DirLookup) { |
428 | bool Invalid; |
429 | const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid); |
430 | (0) . __assert_fail ("!Invalid && \"Attempting to process invalid inclusion\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp", 430, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!Invalid && "Attempting to process invalid inclusion"); |
431 | StringRef FileName = FromFile.getBufferIdentifier(); |
432 | Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts()); |
433 | RawLex.SetCommentRetentionState(false); |
434 | |
435 | StringRef LocalEOL = DetectEOL(FromFile); |
436 | |
437 | |
438 | if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID()) |
439 | WriteLineInfo(FileName, 1, FileType, ""); |
440 | else |
441 | WriteLineInfo(FileName, 1, FileType, " 1"); |
442 | |
443 | if (SM.getFileIDSize(FileId) == 0) |
444 | return; |
445 | |
446 | |
447 | |
448 | unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation()); |
449 | assert(SM.getLineNumber(FileId, NextToWrite) == 1); |
450 | int Line = 1; |
451 | |
452 | Token RawToken; |
453 | RawLex.LexFromRawLexer(RawToken); |
454 | |
455 | |
456 | |
457 | while (RawToken.isNot(tok::eof)) { |
458 | if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) { |
459 | RawLex.setParsingPreprocessorDirective(true); |
460 | Token HashToken = RawToken; |
461 | RawLex.LexFromRawLexer(RawToken); |
462 | if (RawToken.is(tok::raw_identifier)) |
463 | PP.LookUpIdentifierInfo(RawToken); |
464 | if (RawToken.getIdentifierInfo() != nullptr) { |
465 | switch (RawToken.getIdentifierInfo()->getPPKeywordID()) { |
466 | case tok::pp_include: |
467 | case tok::pp_include_next: |
468 | case tok::pp_import: { |
469 | CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, |
470 | Line); |
471 | if (FileId != PP.getPredefinesFileID()) |
472 | WriteLineInfo(FileName, Line - 1, FileType, ""); |
473 | StringRef ; |
474 | SourceLocation Loc = HashToken.getLocation(); |
475 | if (const Module *Mod = FindModuleAtLocation(Loc)) |
476 | WriteImplicitModuleImport(Mod); |
477 | else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) { |
478 | const Module *Mod = FindEnteredModule(Loc); |
479 | if (Mod) |
480 | OS << "#pragma clang module begin " |
481 | << Mod->getFullModuleName(true) << "\n"; |
482 | |
483 | |
484 | Process(Inc->Id, Inc->FileType, Inc->DirLookup); |
485 | |
486 | if (Mod) |
487 | OS << "#pragma clang module end /*" |
488 | << Mod->getFullModuleName(true) << "*/\n"; |
489 | |
490 | |
491 | |
492 | LineInfoExtra = " 2"; |
493 | } |
494 | |
495 | |
496 | WriteLineInfo(FileName, Line, FileType, LineInfoExtra); |
497 | break; |
498 | } |
499 | case tok::pp_pragma: { |
500 | StringRef Identifier = NextIdentifierName(RawLex, RawToken); |
501 | if (Identifier == "clang" || Identifier == "GCC") { |
502 | if (NextIdentifierName(RawLex, RawToken) == "system_header") { |
503 | |
504 | CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, |
505 | NextToWrite, Line); |
506 | |
507 | FileType = SM.getFileCharacteristic(RawToken.getLocation()); |
508 | WriteLineInfo(FileName, Line, FileType); |
509 | } |
510 | } else if (Identifier == "once") { |
511 | |
512 | CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, |
513 | NextToWrite, Line); |
514 | WriteLineInfo(FileName, Line, FileType); |
515 | } |
516 | break; |
517 | } |
518 | case tok::pp_if: |
519 | case tok::pp_elif: { |
520 | bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() == |
521 | tok::pp_elif); |
522 | |
523 | do { |
524 | |
525 | RawLex.LexFromRawLexer(RawToken); |
526 | if (RawToken.is(tok::raw_identifier)) |
527 | PP.LookUpIdentifierInfo(RawToken); |
528 | |
529 | if (RawToken.is(tok::identifier)) { |
530 | bool HasFile; |
531 | SourceLocation Loc = RawToken.getLocation(); |
532 | |
533 | |
534 | if (RawToken.getIdentifierInfo()->isStr("__has_include")) { |
535 | if (!HandleHasInclude(FileId, RawLex, nullptr, RawToken, |
536 | HasFile)) |
537 | continue; |
538 | |
539 | } else if (RawToken.getIdentifierInfo()->isStr( |
540 | "__has_include_next")) { |
541 | if (DirLookup) |
542 | ++DirLookup; |
543 | |
544 | if (!HandleHasInclude(FileId, RawLex, DirLookup, RawToken, |
545 | HasFile)) |
546 | continue; |
547 | } else { |
548 | continue; |
549 | } |
550 | |
551 | |
552 | OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc), |
553 | LocalEOL, Line, false); |
554 | OS << '(' << (int) HasFile << ")/*"; |
555 | OutputContentUpTo(FromFile, NextToWrite, |
556 | SM.getFileOffset(RawToken.getLocation()) + |
557 | RawToken.getLength(), |
558 | LocalEOL, Line, false); |
559 | OS << "*/"; |
560 | } |
561 | } while (RawToken.isNot(tok::eod)); |
562 | if (elif) { |
563 | OutputContentUpTo(FromFile, NextToWrite, |
564 | SM.getFileOffset(RawToken.getLocation()) + |
565 | RawToken.getLength(), |
566 | LocalEOL, Line, true); |
567 | WriteLineInfo(FileName, Line, FileType); |
568 | } |
569 | break; |
570 | } |
571 | case tok::pp_endif: |
572 | case tok::pp_else: { |
573 | |
574 | |
575 | |
576 | |
577 | |
578 | RawLex.SetKeepWhitespaceMode(true); |
579 | do { |
580 | RawLex.LexFromRawLexer(RawToken); |
581 | } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof)); |
582 | OutputContentUpTo(FromFile, NextToWrite, |
583 | SM.getFileOffset(RawToken.getLocation()) + |
584 | RawToken.getLength(), |
585 | LocalEOL, Line, true); |
586 | WriteLineInfo(FileName, Line, FileType); |
587 | RawLex.SetKeepWhitespaceMode(false); |
588 | break; |
589 | } |
590 | default: |
591 | break; |
592 | } |
593 | } |
594 | RawLex.setParsingPreprocessorDirective(false); |
595 | } |
596 | RawLex.LexFromRawLexer(RawToken); |
597 | } |
598 | OutputContentUpTo(FromFile, NextToWrite, |
599 | SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL, |
600 | Line, ); |
601 | } |
602 | |
603 | |
604 | void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, |
605 | const PreprocessorOutputOptions &Opts) { |
606 | SourceManager &SM = PP.getSourceManager(); |
607 | InclusionRewriter *Rewrite = new InclusionRewriter( |
608 | PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives); |
609 | Rewrite->detectMainFileEOL(); |
610 | |
611 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite)); |
612 | PP.IgnorePragmas(); |
613 | |
614 | |
615 | |
616 | PP.EnterMainSourceFile(); |
617 | Token Tok; |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | PP.SetMacroExpansionOnlyInDirectives(); |
624 | do { |
625 | PP.Lex(Tok); |
626 | if (Tok.is(tok::annot_module_begin)) |
627 | Rewrite->handleModuleBegin(Tok); |
628 | } while (Tok.isNot(tok::eof)); |
629 | Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID())); |
630 | Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User, nullptr); |
631 | Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User, nullptr); |
632 | OS->flush(); |
633 | } |
634 | |