#include "clang/Rewrite/Frontend/Rewriters.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/PreprocessorOutputOptions.h"
#include "clang/Lex/Pragma.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace llvm;
namespace {
class InclusionRewriter : public PPCallbacks {
struct IncludedFile {
FileID Id;
SrcMgr::CharacteristicKind FileType;
IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType)
: Id(Id), FileType(FileType) {}
};
Preprocessor &PP; SourceManager &SM; raw_ostream &OS; StringRef MainEOL; llvm::MemoryBufferRef PredefinesBuffer; bool ShowLineMarkers; bool UseLineDirectives; std::map<SourceLocation, IncludedFile> FileIncludes;
std::map<SourceLocation, const Module *> ModuleIncludes;
std::map<SourceLocation, const Module *> ModuleEntryIncludes;
std::map<SourceLocation, bool> IfConditions;
SourceLocation LastInclusionLocation;
public:
InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
bool UseLineDirectives);
void Process(FileID FileId, SrcMgr::CharacteristicKind FileType);
void setPredefinesBuffer(const llvm::MemoryBufferRef &Buf) {
PredefinesBuffer = Buf;
}
void detectMainFileEOL();
void handleModuleBegin(Token &Tok) {
assert(Tok.getKind() == tok::annot_module_begin);
ModuleEntryIncludes.insert(
{Tok.getLocation(), (Module *)Tok.getAnnotationValue()});
}
private:
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType,
FileID PrevFID) override;
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override;
void Elif(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
void WriteLineInfo(StringRef Filename, int Line,
SrcMgr::CharacteristicKind FileType,
StringRef Extra = StringRef());
void WriteImplicitModuleImport(const Module *Mod);
void OutputContentUpTo(const MemoryBufferRef &FromFile, unsigned &WriteFrom,
unsigned WriteTo, StringRef EOL, int &lines,
bool EnsureNewline);
void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
const MemoryBufferRef &FromFile, StringRef EOL,
unsigned &NextToWrite, int &Lines);
const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
const Module *FindModuleAtLocation(SourceLocation Loc) const;
const Module *FindEnteredModule(SourceLocation Loc) const;
bool IsIfAtLocationTrue(SourceLocation Loc) const;
StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
};
}
InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
bool ShowLineMarkers,
bool UseLineDirectives)
: PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
ShowLineMarkers(ShowLineMarkers), UseLineDirectives(UseLineDirectives),
LastInclusionLocation(SourceLocation()) {}
void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
SrcMgr::CharacteristicKind FileType,
StringRef Extra) {
if (!ShowLineMarkers)
return;
if (UseLineDirectives) {
OS << "#line" << ' ' << Line << ' ' << '"';
OS.write_escaped(Filename);
OS << '"';
} else {
OS << '#' << ' ' << Line << ' ' << '"';
OS.write_escaped(Filename);
OS << '"';
if (!Extra.empty())
OS << Extra;
if (FileType == SrcMgr::C_System)
OS << " 3";
else if (FileType == SrcMgr::C_ExternCSystem)
OS << " 3 4";
}
OS << MainEOL;
}
void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
OS << "#pragma clang module import " << Mod->getFullModuleName(true)
<< " /* clang -frewrite-includes: implicit import */" << MainEOL;
}
void InclusionRewriter::FileChanged(SourceLocation Loc,
FileChangeReason Reason,
SrcMgr::CharacteristicKind NewFileType,
FileID) {
if (Reason != EnterFile)
return;
if (LastInclusionLocation.isInvalid())
return;
FileID Id = FullSourceLoc(Loc, SM).getFileID();
auto P = FileIncludes.insert(
std::make_pair(LastInclusionLocation, IncludedFile(Id, NewFileType)));
(void)P;
assert(P.second && "Unexpected revisitation of the same include directive");
LastInclusionLocation = SourceLocation();
}
void InclusionRewriter::FileSkipped(const FileEntryRef & ,
const Token & ,
SrcMgr::CharacteristicKind ) {
assert(LastInclusionLocation.isValid() &&
"A file, that wasn't found via an inclusion directive, was skipped");
LastInclusionLocation = SourceLocation();
}
void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
const Token &,
StringRef ,
bool ,
CharSourceRange ,
Optional<FileEntryRef> ,
StringRef ,
StringRef ,
const Module *Imported,
SrcMgr::CharacteristicKind FileType){
if (Imported) {
auto P = ModuleIncludes.insert(std::make_pair(HashLoc, Imported));
(void)P;
assert(P.second && "Unexpected revisitation of the same include directive");
} else
LastInclusionLocation = HashLoc;
}
void InclusionRewriter::If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) {
auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
(void)P;
assert(P.second && "Unexpected revisitation of the same if directive");
}
void InclusionRewriter::Elif(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue,
SourceLocation IfLoc) {
auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
(void)P;
assert(P.second && "Unexpected revisitation of the same elif directive");
}
const InclusionRewriter::IncludedFile *
InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
const auto I = FileIncludes.find(Loc);
if (I != FileIncludes.end())
return &I->second;
return nullptr;
}
const Module *
InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
const auto I = ModuleIncludes.find(Loc);
if (I != ModuleIncludes.end())
return I->second;
return nullptr;
}
const Module *
InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
const auto I = ModuleEntryIncludes.find(Loc);
if (I != ModuleEntryIncludes.end())
return I->second;
return nullptr;
}
bool InclusionRewriter::IsIfAtLocationTrue(SourceLocation Loc) const {
const auto I = IfConditions.find(Loc);
if (I != IfConditions.end())
return I->second;
return false;
}
void InclusionRewriter::detectMainFileEOL() {
Optional<MemoryBufferRef> FromFile = *SM.getBufferOrNone(SM.getMainFileID());
assert(FromFile);
if (!FromFile)
return; MainEOL = FromFile->getBuffer().detectEOL();
}
void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef &FromFile,
unsigned &WriteFrom, unsigned WriteTo,
StringRef LocalEOL, int &Line,
bool EnsureNewline) {
if (WriteTo <= WriteFrom)
return;
if (FromFile == PredefinesBuffer) {
WriteFrom = WriteTo;
return;
}
if (LocalEOL.size() == 2 &&
LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
WriteTo++;
StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
WriteTo - WriteFrom);
if (MainEOL == LocalEOL) {
OS << TextToWrite;
Line += TextToWrite.count(LocalEOL);
if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
OS << MainEOL;
} else {
StringRef Rest = TextToWrite;
while (!Rest.empty()) {
StringRef LineText;
std::tie(LineText, Rest) = Rest.split(LocalEOL);
OS << LineText;
Line++;
if (!Rest.empty())
OS << MainEOL;
}
if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
OS << MainEOL;
}
WriteFrom = WriteTo;
}
void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
const Token &StartToken,
const MemoryBufferRef &FromFile,
StringRef LocalEOL,
unsigned &NextToWrite, int &Line) {
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
false);
Token DirectiveToken;
do {
DirectiveLex.LexFromRawLexer(DirectiveToken);
} while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
if (FromFile == PredefinesBuffer) {
return;
}
OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(DirectiveToken.getLocation()) +
DirectiveToken.getLength(),
LocalEOL, Line, true);
OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
}
StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
Token &RawToken) {
RawLex.LexFromRawLexer(RawToken);
if (RawToken.is(tok::raw_identifier))
PP.LookUpIdentifierInfo(RawToken);
if (RawToken.is(tok::identifier))
return RawToken.getIdentifierInfo()->getName();
return StringRef();
}
void InclusionRewriter::Process(FileID FileId,
SrcMgr::CharacteristicKind FileType) {
MemoryBufferRef FromFile;
{
auto B = SM.getBufferOrNone(FileId);
assert(B && "Attempting to process invalid inclusion");
if (B)
FromFile = *B;
}
StringRef FileName = FromFile.getBufferIdentifier();
Lexer RawLex(FileId, FromFile, PP.getSourceManager(), PP.getLangOpts());
RawLex.SetCommentRetentionState(false);
StringRef LocalEOL = FromFile.getBuffer().detectEOL();
if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
WriteLineInfo(FileName, 1, FileType, "");
else
WriteLineInfo(FileName, 1, FileType, " 1");
if (SM.getFileIDSize(FileId) == 0)
return;
unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
assert(SM.getLineNumber(FileId, NextToWrite) == 1);
int Line = 1;
Token RawToken;
RawLex.LexFromRawLexer(RawToken);
while (RawToken.isNot(tok::eof)) {
if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
RawLex.setParsingPreprocessorDirective(true);
Token HashToken = RawToken;
RawLex.LexFromRawLexer(RawToken);
if (RawToken.is(tok::raw_identifier))
PP.LookUpIdentifierInfo(RawToken);
if (RawToken.getIdentifierInfo() != nullptr) {
switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
case tok::pp_include:
case tok::pp_include_next:
case tok::pp_import: {
CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
Line);
if (FileId != PP.getPredefinesFileID())
WriteLineInfo(FileName, Line - 1, FileType, "");
StringRef LineInfoExtra;
SourceLocation Loc = HashToken.getLocation();
if (const Module *Mod = FindModuleAtLocation(Loc))
WriteImplicitModuleImport(Mod);
else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
const Module *Mod = FindEnteredModule(Loc);
if (Mod)
OS << "#pragma clang module begin "
<< Mod->getFullModuleName(true) << "\n";
Process(Inc->Id, Inc->FileType);
if (Mod)
OS << "#pragma clang module end /*"
<< Mod->getFullModuleName(true) << "*/\n";
LineInfoExtra = " 2";
}
WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
break;
}
case tok::pp_pragma: {
StringRef Identifier = NextIdentifierName(RawLex, RawToken);
if (Identifier == "clang" || Identifier == "GCC") {
if (NextIdentifierName(RawLex, RawToken) == "system_header") {
CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
NextToWrite, Line);
FileType = SM.getFileCharacteristic(RawToken.getLocation());
WriteLineInfo(FileName, Line, FileType);
}
} else if (Identifier == "once") {
CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
NextToWrite, Line);
WriteLineInfo(FileName, Line, FileType);
}
break;
}
case tok::pp_if:
case tok::pp_elif: {
bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
tok::pp_elif);
bool isTrue = IsIfAtLocationTrue(RawToken.getLocation());
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(HashToken.getLocation()),
LocalEOL, Line, true);
do {
RawLex.LexFromRawLexer(RawToken);
} while (!RawToken.is(tok::eod) && RawToken.isNot(tok::eof));
OS << "#if 0 /* disabled by -frewrite-includes */" << MainEOL;
if (elif) {
OS << "#if 0" << MainEOL;
}
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(RawToken.getLocation()) +
RawToken.getLength(),
LocalEOL, Line, true);
OS << "#endif" << MainEOL;
OS << "#endif /* disabled by -frewrite-includes */" << MainEOL;
OS << (elif ? "#elif " : "#if ") << (isTrue ? "1" : "0")
<< " /* evaluated by -frewrite-includes */" << MainEOL;
WriteLineInfo(FileName, Line, FileType);
break;
}
case tok::pp_endif:
case tok::pp_else: {
RawLex.SetKeepWhitespaceMode(true);
do {
RawLex.LexFromRawLexer(RawToken);
} while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(RawToken.getLocation()) +
RawToken.getLength(),
LocalEOL, Line, true);
WriteLineInfo(FileName, Line, FileType);
RawLex.SetKeepWhitespaceMode(false);
break;
}
default:
break;
}
}
RawLex.setParsingPreprocessorDirective(false);
}
RawLex.LexFromRawLexer(RawToken);
}
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
Line, true);
}
void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
const PreprocessorOutputOptions &Opts) {
SourceManager &SM = PP.getSourceManager();
InclusionRewriter *Rewrite = new InclusionRewriter(
PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
Rewrite->detectMainFileEOL();
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
PP.IgnorePragmas();
PP.EnterMainSourceFile();
Token Tok;
PP.SetMacroExpansionOnlyInDirectives();
do {
PP.Lex(Tok);
if (Tok.is(tok::annot_module_begin))
Rewrite->handleModuleBegin(Tok);
} while (Tok.isNot(tok::eof));
Rewrite->setPredefinesBuffer(SM.getBufferOrFake(PP.getPredefinesFileID()));
Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User);
Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User);
OS->flush();
}