;
41llvm::MemoryBufferRef PredefinesBuffer;
43 boolUseLineDirectives;
45std::map<SourceLocation, IncludedFile> FileIncludes;
47std::map<SourceLocation, const Module *> ModuleIncludes;
49std::map<SourceLocation, const Module *> ModuleEntryIncludes;
51std::map<SourceLocation, bool> IfConditions;
56InclusionRewriter(
Preprocessor&PP, raw_ostream &OS,
boolShowLineMarkers,
57 boolUseLineDirectives);
59 voidsetPredefinesBuffer(
constllvm::MemoryBufferRef &Buf) {
60PredefinesBuffer = Buf;
62 voiddetectMainFileEOL();
63 voidhandleModuleBegin(
Token&Tok) {
64assert(Tok.
getKind() == tok::annot_module_begin);
65ModuleEntryIncludes.insert(
78StringRef RelativePath,
const Module*SuggestedModule,
82ConditionValueKind ConditionValue)
override;
84ConditionValueKind ConditionValue,
SourceLocationIfLoc)
override;
85 voidWriteLineInfo(StringRef
Filename,
intLine,
87StringRef Extra = StringRef());
88 voidWriteImplicitModuleImport(
const Module*Mod);
89 voidOutputContentUpTo(
constMemoryBufferRef &FromFile,
unsigned&WriteFrom,
90 unsignedWriteTo, StringRef EOL,
int&lines,
92 voidCommentOutDirective(
Lexer&DirectivesLex,
const Token&StartToken,
93 constMemoryBufferRef &FromFile, StringRef EOL,
94 unsigned&NextToWrite,
int&Lines,
95 constIncludedFile *Inc =
nullptr);
97StringRef getIncludedFileName(
constIncludedFile *Inc)
const;
101StringRef NextIdentifierName(
Lexer&RawLex,
Token&RawToken);
107InclusionRewriter::InclusionRewriter(
Preprocessor&PP, raw_ostream &OS,
108 boolShowLineMarkers,
109 boolUseLineDirectives)
110: PP(PP),
SM(PP.getSourceManager()), OS(OS), MainEOL(
"\n"),
111ShowLineMarkers(ShowLineMarkers), UseLineDirectives(UseLineDirectives),
118voidInclusionRewriter::WriteLineInfo(StringRef
Filename,
intLine,
121 if(!ShowLineMarkers)
123 if(UseLineDirectives) {
124OS <<
"#line"<<
' '<< Line <<
' '<<
'"';
130OS <<
'#'<<
' '<< Line <<
' '<<
'"';
147voidInclusionRewriter::WriteImplicitModuleImport(
const Module*Mod) {
149<<
" /* clang -frewrite-includes: implicit import */"<< MainEOL;
155FileChangeReason Reason,
158 if(Reason != EnterFile)
164 auto P= FileIncludes.insert(
165std::make_pair(LastInclusionLocation, IncludedFile(
Id, NewFileType)));
167assert(
P.second &&
"Unexpected revisitation of the same include directive");
173voidInclusionRewriter::FileSkipped(
const FileEntryRef&
,
176assert(LastInclusionLocation.
isValid() &&
177 "A file, that wasn't found via an inclusion directive, was skipped");
188voidInclusionRewriter::InclusionDirective(
192StringRef
, StringRef
,
193 const Module*SuggestedModule,
boolModuleImported,
195 if(ModuleImported) {
196 auto P= ModuleIncludes.insert(std::make_pair(HashLoc, SuggestedModule));
198assert(
P.second &&
"Unexpected revisitation of the same include directive");
200LastInclusionLocation = HashLoc;
204ConditionValueKind ConditionValue) {
205 auto P= IfConditions.insert(std::make_pair(
Loc, ConditionValue == CVK_True));
207assert(
P.second &&
"Unexpected revisitation of the same if directive");
211ConditionValueKind ConditionValue,
213 auto P= IfConditions.insert(std::make_pair(
Loc, ConditionValue == CVK_True));
215assert(
P.second &&
"Unexpected revisitation of the same elif directive");
220constInclusionRewriter::IncludedFile *
222 const autoI = FileIncludes.find(
Loc);
223 if(I != FileIncludes.end())
232 const autoI = ModuleIncludes.find(
Loc);
233 if(I != ModuleIncludes.end())
242 const autoI = ModuleEntryIncludes.find(
Loc);
243 if(I != ModuleEntryIncludes.end())
249 const autoI = IfConditions.find(
Loc);
250 if(I != IfConditions.end())
255voidInclusionRewriter::detectMainFileEOL() {
256std::optional<MemoryBufferRef> FromFile =
257*
SM.getBufferOrNone(
SM.getMainFileID());
261MainEOL = FromFile->getBuffer().detectEOL();
266voidInclusionRewriter::OutputContentUpTo(
constMemoryBufferRef &FromFile,
267 unsigned&WriteFrom,
unsignedWriteTo,
268StringRef LocalEOL,
int&Line,
269 boolEnsureNewline) {
270 if(WriteTo <= WriteFrom)
272 if(FromFile == PredefinesBuffer) {
281 if(LocalEOL.size() == 2 &&
282LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
283LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
286StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
287WriteTo - WriteFrom);
289Line += TextToWrite.count(LocalEOL);
291 if(MainEOL == LocalEOL) {
295StringRef Rest = TextToWrite;
296 while(!Rest.empty()) {
298 size_tIdx = Rest.find(LocalEOL);
299StringRef LineText = Rest.substr(0, Idx);
301 if(Idx != StringRef::npos) {
305Idx += LocalEOL.size();
309Rest = Rest.substr(Idx);
312 if(EnsureNewline && !TextToWrite.ends_with(LocalEOL))
319InclusionRewriter::getIncludedFileName(
constIncludedFile *Inc)
const{
321 autoB =
SM.getBufferOrNone(
Inc->Id);
322assert(B &&
"Attempting to process invalid inclusion");
324 returnllvm::sys::path::filename(B->getBufferIdentifier());
334voidInclusionRewriter::CommentOutDirective(
Lexer&DirectiveLex,
335 const Token&StartToken,
336 constMemoryBufferRef &FromFile,
338 unsigned&NextToWrite,
int&Line,
339 constIncludedFile *Inc) {
340OutputContentUpTo(FromFile, NextToWrite,
341 SM.getFileOffset(StartToken.
getLocation()), LocalEOL, Line,
343 TokenDirectiveToken;
346}
while(!DirectiveToken.
is(tok::eod) && DirectiveToken.
isNot(tok::eof));
347 if(FromFile == PredefinesBuffer) {
352OS <<
"#if defined(__CLANG_REWRITTEN_INCLUDES) ";
354OS <<
"|| defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) ";
355OS <<
"/* "<< getIncludedFileName(Inc);
359OS <<
" expanded by -frewrite-includes */"<< MainEOL;
360OutputContentUpTo(FromFile, NextToWrite,
363LocalEOL, Line,
true);
364OS << (
Inc?
"#else /* ":
"#endif /*") << getIncludedFileName(Inc)
365<<
" expanded by -frewrite-includes */"<< MainEOL;
369StringRef InclusionRewriter::NextIdentifierName(
Lexer&RawLex,
372 if(RawToken.
is(tok::raw_identifier))
374 if(RawToken.
is(tok::identifier))
381voidInclusionRewriter::Process(
FileIDFileId,
383MemoryBufferRef FromFile;
385 autoB =
SM.getBufferOrNone(FileId);
386assert(B &&
"Attempting to process invalid inclusion");
390StringRef
FileName= FromFile.getBufferIdentifier();
394StringRef LocalEOL = FromFile.getBuffer().detectEOL();
402 if(
SM.getFileIDSize(FileId) == 0)
408assert(
SM.getLineNumber(FileId, NextToWrite) == 1);
416 while(RawToken.
isNot(tok::eof)) {
419 TokenHashToken = RawToken;
421 if(RawToken.
is(tok::raw_identifier))
425 casetok::pp_include:
426 casetok::pp_include_next:
427 casetok::pp_import: {
429 constIncludedFile *
Inc= FindIncludeAtLocation(
Loc);
430CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
431NextToWrite, Line, Inc);
434StringRef LineInfoExtra;
435 if(
const Module*Mod = FindModuleAtLocation(
Loc))
436WriteImplicitModuleImport(Mod);
438 const Module*Mod = FindEnteredModule(
Loc);
440OS <<
"#pragma clang module begin " 444Process(
Inc->Id,
Inc->FileType);
447OS <<
"#pragma clang module end /*" 450 if(FromFile != PredefinesBuffer) {
451OS <<
"#endif /* "<< getIncludedFileName(Inc)
452<<
" expanded by -frewrite-includes */"<< LocalEOL;
457LineInfoExtra =
" 2";
464 casetok::pp_pragma: {
465StringRef
Identifier= NextIdentifierName(RawLex, RawToken);
467 if(NextIdentifierName(RawLex, RawToken) ==
"system_header") {
469CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
477CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
487 boolisTrue = IsIfAtLocationTrue(RawToken.
getLocation());
488OutputContentUpTo(FromFile, NextToWrite,
490LocalEOL, Line,
true);
493}
while(!RawToken.
is(tok::eod) && RawToken.
isNot(tok::eof));
500OS <<
"#if 0 /* disabled by -frewrite-includes */"<< MainEOL;
502OS <<
"#if 0"<< MainEOL;
504OutputContentUpTo(FromFile, NextToWrite,
507LocalEOL, Line,
true);
509OS <<
"#endif"<< MainEOL;
510OS <<
"#endif /* disabled by -frewrite-includes */"<< MainEOL;
511OS << (elif ?
"#elif ":
"#if ") << (isTrue ?
"1":
"0")
512<<
" /* evaluated by -frewrite-includes */"<< MainEOL;
526}
while(RawToken.
isNot(tok::eod) && RawToken.
isNot(tok::eof));
527OutputContentUpTo(FromFile, NextToWrite,
530LocalEOL, Line,
true);
543OutputContentUpTo(FromFile, NextToWrite,
544 SM.getFileOffset(
SM.getLocForEndOfFile(FileId)), LocalEOL,
552InclusionRewriter *
Rewrite=
newInclusionRewriter(
571 if(Tok.
is(tok::annot_module_begin))
572 Rewrite->handleModuleBegin(Tok);
573}
while(Tok.
isNot(tok::eof));
llvm::MachO::FileType FileType
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 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...
bool isSystem(CharacteristicKind CK)
Determine whether a file / directory characteristic is for system code.
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 ...
The JSON file list parser is used to communicate input to InstallAPI.
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
@ If
'if' clause, allowed on all the Compute Constructs, Data Constructs, Executable Constructs,...
void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
RewriteIncludesInInput - Implement -frewrite-includes mode.
Diagnostic wrappers for TextAPI types for error reporting.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4