19#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/raw_ostream.h"
35 :
Id(
Id), FileType(FileType) {}
41 llvm::MemoryBufferRef PredefinesBuffer;
43 bool UseLineDirectives;
45 std::map<SourceLocation, IncludedFile> FileIncludes;
47 std::map<SourceLocation, const Module *> ModuleIncludes;
49 std::map<SourceLocation, const Module *> ModuleEntryIncludes;
51 std::map<SourceLocation, bool> IfConditions;
56 InclusionRewriter(
Preprocessor &PP, raw_ostream &OS,
bool ShowLineMarkers,
57 bool UseLineDirectives);
59 void setPredefinesBuffer(
const llvm::MemoryBufferRef &Buf) {
60 PredefinesBuffer = Buf;
62 void detectMainFileEOL();
63 void handleModuleBegin(
Token &Tok) {
64 assert(Tok.
getKind() == tok::annot_module_begin);
65 ModuleEntryIncludes.insert(
75 StringRef FileName,
bool IsAngled,
78 StringRef RelativePath,
const Module *Imported,
81 ConditionValueKind ConditionValue)
override;
84 void WriteLineInfo(StringRef
Filename,
int Line,
86 StringRef Extra = StringRef());
87 void WriteImplicitModuleImport(
const Module *Mod);
88 void OutputContentUpTo(
const MemoryBufferRef &FromFile,
unsigned &WriteFrom,
89 unsigned WriteTo, StringRef EOL,
int &lines,
91 void CommentOutDirective(
Lexer &DirectivesLex,
const Token &StartToken,
92 const MemoryBufferRef &FromFile, StringRef EOL,
93 unsigned &NextToWrite,
int &Lines);
94 const IncludedFile *FindIncludeAtLocation(
SourceLocation Loc)
const;
98 StringRef NextIdentifierName(
Lexer &RawLex,
Token &RawToken);
104InclusionRewriter::InclusionRewriter(
Preprocessor &PP, raw_ostream &OS,
105 bool ShowLineMarkers,
106 bool UseLineDirectives)
107 : PP(PP),
SM(PP.getSourceManager()),
OS(
OS), MainEOL(
"\n"),
108 ShowLineMarkers(ShowLineMarkers), UseLineDirectives(UseLineDirectives),
115void InclusionRewriter::WriteLineInfo(StringRef
Filename,
int Line,
118 if (!ShowLineMarkers)
120 if (UseLineDirectives) {
121 OS <<
"#line" <<
' ' << Line <<
' ' <<
'"';
127 OS <<
'#' <<
' ' << Line <<
' ' <<
'"';
144void InclusionRewriter::WriteImplicitModuleImport(
const Module *Mod) {
146 <<
" /* clang -frewrite-includes: implicit import */" << MainEOL;
152 FileChangeReason Reason,
155 if (Reason != EnterFile)
161 auto P = FileIncludes.insert(
162 std::make_pair(LastInclusionLocation, IncludedFile(
Id, NewFileType)));
164 assert(
P.second &&
"Unexpected revisitation of the same include directive");
170void InclusionRewriter::FileSkipped(
const FileEntryRef & ,
173 assert(LastInclusionLocation.
isValid() &&
174 "A file, that wasn't found via an inclusion directive, was skipped");
185void InclusionRewriter::InclusionDirective(
189 StringRef , StringRef ,
192 auto P = ModuleIncludes.insert(std::make_pair(HashLoc, Imported));
194 assert(
P.second &&
"Unexpected revisitation of the same include directive");
196 LastInclusionLocation = HashLoc;
200 ConditionValueKind ConditionValue) {
201 auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
203 assert(
P.second &&
"Unexpected revisitation of the same if directive");
207 ConditionValueKind ConditionValue,
209 auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
211 assert(
P.second &&
"Unexpected revisitation of the same elif directive");
216const InclusionRewriter::IncludedFile *
217InclusionRewriter::FindIncludeAtLocation(
SourceLocation Loc)
const {
218 const auto I = FileIncludes.find(Loc);
219 if (I != FileIncludes.end())
227InclusionRewriter::FindModuleAtLocation(
SourceLocation Loc)
const {
228 const auto I = ModuleIncludes.find(Loc);
229 if (I != ModuleIncludes.end())
238 const auto I = ModuleEntryIncludes.find(Loc);
239 if (I != ModuleEntryIncludes.end())
244bool InclusionRewriter::IsIfAtLocationTrue(
SourceLocation Loc)
const {
245 const auto I = IfConditions.find(Loc);
246 if (I != IfConditions.end())
251void InclusionRewriter::detectMainFileEOL() {
252 std::optional<MemoryBufferRef> FromFile =
253 *
SM.getBufferOrNone(
SM.getMainFileID());
257 MainEOL = FromFile->getBuffer().detectEOL();
262void InclusionRewriter::OutputContentUpTo(
const MemoryBufferRef &FromFile,
263 unsigned &WriteFrom,
unsigned WriteTo,
264 StringRef LocalEOL,
int &Line,
265 bool EnsureNewline) {
266 if (WriteTo <= WriteFrom)
268 if (FromFile == PredefinesBuffer) {
277 if (LocalEOL.size() == 2 &&
278 LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
279 LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
282 StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
283 WriteTo - WriteFrom);
285 Line += TextToWrite.count(LocalEOL);
287 if (MainEOL == LocalEOL) {
291 StringRef Rest = TextToWrite;
292 while (!Rest.empty()) {
294 size_t Idx = Rest.find(LocalEOL);
295 StringRef LineText = Rest.substr(0, Idx);
297 if (Idx != StringRef::npos) {
301 Idx += LocalEOL.size();
305 Rest = Rest.substr(Idx);
308 if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
319void InclusionRewriter::CommentOutDirective(
Lexer &DirectiveLex,
320 const Token &StartToken,
321 const MemoryBufferRef &FromFile,
323 unsigned &NextToWrite,
int &Line) {
324 OutputContentUpTo(FromFile, NextToWrite,
327 Token DirectiveToken;
330 }
while (!DirectiveToken.
is(tok::eod) && DirectiveToken.
isNot(tok::eof));
331 if (FromFile == PredefinesBuffer) {
335 OS <<
"#if 0 /* expanded by -frewrite-includes */" << MainEOL;
336 OutputContentUpTo(FromFile, NextToWrite,
339 LocalEOL, Line,
true);
340 OS <<
"#endif /* expanded by -frewrite-includes */" << MainEOL;
344StringRef InclusionRewriter::NextIdentifierName(
Lexer &RawLex,
347 if (RawToken.
is(tok::raw_identifier))
349 if (RawToken.
is(tok::identifier))
356void InclusionRewriter::Process(
FileID FileId,
358 MemoryBufferRef FromFile;
360 auto B =
SM.getBufferOrNone(FileId);
361 assert(B &&
"Attempting to process invalid inclusion");
365 StringRef FileName = FromFile.getBufferIdentifier();
369 StringRef LocalEOL = FromFile.getBuffer().detectEOL();
373 WriteLineInfo(FileName, 1, FileType,
"");
375 WriteLineInfo(FileName, 1, FileType,
" 1");
377 if (
SM.getFileIDSize(FileId) == 0)
383 assert(
SM.getLineNumber(FileId, NextToWrite) == 1);
391 while (RawToken.
isNot(tok::eof)) {
394 Token HashToken = RawToken;
396 if (RawToken.
is(tok::raw_identifier))
400 case tok::pp_include:
401 case tok::pp_include_next:
402 case tok::pp_import: {
403 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
406 WriteLineInfo(FileName, Line - 1, FileType,
"");
407 StringRef LineInfoExtra;
409 if (
const Module *Mod = FindModuleAtLocation(Loc))
410 WriteImplicitModuleImport(Mod);
411 else if (
const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
412 const Module *Mod = FindEnteredModule(Loc);
414 OS <<
"#pragma clang module begin "
418 Process(
Inc->Id,
Inc->FileType);
421 OS <<
"#pragma clang module end /*"
426 LineInfoExtra =
" 2";
430 WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
433 case tok::pp_pragma: {
434 StringRef
Identifier = NextIdentifierName(RawLex, RawToken);
436 if (NextIdentifierName(RawLex, RawToken) ==
"system_header") {
438 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
442 WriteLineInfo(FileName, Line, FileType);
446 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
448 WriteLineInfo(FileName, Line, FileType);
456 bool isTrue = IsIfAtLocationTrue(RawToken.
getLocation());
457 OutputContentUpTo(FromFile, NextToWrite,
459 LocalEOL, Line,
true);
462 }
while (!RawToken.
is(tok::eod) && RawToken.
isNot(tok::eof));
469 OS <<
"#if 0 /* disabled by -frewrite-includes */" << MainEOL;
471 OS <<
"#if 0" << MainEOL;
473 OutputContentUpTo(FromFile, NextToWrite,
476 LocalEOL, Line,
true);
478 OS <<
"#endif" << MainEOL;
479 OS <<
"#endif /* disabled by -frewrite-includes */" << MainEOL;
480 OS << (elif ?
"#elif " :
"#if ") << (isTrue ?
"1" :
"0")
481 <<
" /* evaluated by -frewrite-includes */" << MainEOL;
482 WriteLineInfo(FileName, Line, FileType);
495 }
while (RawToken.
isNot(tok::eod) && RawToken.
isNot(tok::eof));
496 OutputContentUpTo(FromFile, NextToWrite,
499 LocalEOL, Line,
true);
500 WriteLineInfo(FileName, Line, FileType);
512 OutputContentUpTo(FromFile, NextToWrite,
513 SM.getFileOffset(
SM.getLocForEndOfFile(FileId)), LocalEOL,
521 InclusionRewriter *
Rewrite =
new InclusionRewriter(
540 if (Tok.
is(tok::annot_module_begin))
541 Rewrite->handleModuleBegin(Tok);
542 }
while (Tok.
isNot(tok::eof));
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
Represents a character-granular source range.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
A SourceLocation and its associated SourceManager.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
StringRef getName() const
Return the actual identifier string.
Record the location of an inclusion directive, such as an #include or #import statement.
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
void SetKeepWhitespaceMode(bool Val)
SetKeepWhitespaceMode - This method lets clients enable or disable whitespace retention mode.
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
void SetCommentRetentionState(bool Mode)
SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.
SourceLocation getSourceLocation(const char *Loc, unsigned TokLen=1) const
getSourceLocation - Return a source location identifier for the specified offset in the current file.
Describes a module or submodule.
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc)
Hook called whenever an #elif is seen.
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID=FileID())
Callback invoked whenever a source file is entered or exited.
virtual void If(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue)
Hook called whenever an #if is seen.
virtual void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType)
Callback invoked whenever a source file is skipped as the result of header guard optimization.
void setParsingPreprocessorDirective(bool f)
Inform the lexer whether or not we are currently lexing a preprocessor directive.
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
unsigned UseLineDirectives
Use #line instead of GCC-style # N.
unsigned ShowLineMarkers
Show #line markers.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void IgnorePragmas()
Install empty handlers for all pragmas (making them ignored).
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void Lex(Token &Result)
Lex the next token for this preprocessor.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
SourceManager & getSourceManager() const
void SetMacroExpansionOnlyInDirectives()
Disables macro expansion everywhere except for preprocessor directives.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
const LangOptions & getLangOpts() const
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Token - This structure provides full information about a lexed token.
IdentifierInfo * getIdentifierInfo() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
void * getAnnotationValue() const
tok::TokenKind getKind() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool isNot(tok::TokenKind K) const
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool Inc(InterpState &S, CodePtr OpPC)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
RewriteIncludesInInput - Implement -frewrite-includes mode.
YAML serialization mapping.