Merge pull request #198 from AWoloszyn/update-includer

Updated the includer interface to allow relative includes.
This commit is contained in:
John Kessenich 2016-03-21 13:23:20 -06:00
commit 56368b68ed
7 changed files with 220 additions and 54 deletions

View file

@ -611,24 +611,28 @@ int TPpContext::CPPinclude(TPpToken* ppToken)
if (token != '\n' && token != EndOfInput) {
parseContext.ppError(ppToken->loc, "extra content after file designation", "#include", "");
} else {
auto include = includer.include(filename.c_str());
std::string sourceName = include.first;
std::string replacement = include.second;
if (!sourceName.empty()) {
if (!replacement.empty()) {
TShader::Includer::IncludeResult* res = includer.include(filename.c_str(), TShader::Includer::EIncludeRelative, currentSourceFile.c_str(), includeStack.size() + 1);
if (res && !res->file_name.empty()) {
if (res->file_data && res->file_length) {
const bool forNextLine = parseContext.lineDirectiveShouldSetNextLine();
std::ostringstream content;
content << "#line " << forNextLine << " " << "\"" << sourceName << "\"\n";
content << replacement << (replacement.back() == '\n' ? "" : "\n");
content << "#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n";
pushInput(new TokenizableString(directiveLoc, content.str(), this));
std::ostringstream prologue;
std::ostringstream epilogue;
prologue << "#line " << forNextLine << " " << "\"" << res->file_name << "\"\n";
epilogue << (res->file_data[res->file_length - 1] == '\n'? "" : "\n") << "#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n";
pushInput(new TokenizableIncludeFile(directiveLoc, prologue.str(), res, epilogue.str(), this));
}
// At EOF, there's no "current" location anymore.
if (token != EndOfInput) parseContext.setCurrentColumn(0);
// Don't accidentally return EndOfInput, which will end all preprocessing.
return '\n';
} else {
parseContext.ppError(directiveLoc, replacement.c_str(), "#include", "");
std::string message =
res ? std::string(res->file_data, res->file_length)
: std::string("Could not process include directive");
parseContext.ppError(directiveLoc, message.c_str(), "#include", "");
if (res) {
includer.releaseInclude(res);
}
}
}
}

View file

@ -83,8 +83,10 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang {
TPpContext::TPpContext(TParseContext& pc, const TShader::Includer& inclr) :
preamble(0), strings(0), parseContext(pc), includer(inclr), inComment(false)
TPpContext::TPpContext(TParseContext& pc, const std::string& rootFileName, TShader::Includer& inclr) :
preamble(0), strings(0), parseContext(pc), includer(inclr), inComment(false),
rootFileName(rootFileName),
currentSourceFile(rootFileName)
{
InitAtomTable();
InitScanner();

View file

@ -78,6 +78,7 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef PPCONTEXT_H
#define PPCONTEXT_H
#include <stack>
#include <unordered_map>
#include "../ParseHelper.h"
@ -121,7 +122,7 @@ class TInputScanner;
// Don't expect too much in terms of OO design.
class TPpContext {
public:
TPpContext(TParseContext&, const TShader::Includer&);
TPpContext(TParseContext&, const std::string& rootFileName, TShader::Includer&);
virtual ~TPpContext();
void setPreamble(const char* preamble, size_t length);
@ -290,7 +291,7 @@ protected:
//
// Used to obtain #include content.
const TShader::Includer& includer;
TShader::Includer& includer;
int InitCPP();
int CPPdefine(TPpToken * ppToken);
@ -430,16 +431,30 @@ protected:
TInputScanner* input;
};
// Holds a string that can be tokenized via the tInput interface.
class TokenizableString : public tInput {
// Holds a reference to included file data, as well as a
// prologue and an epilogue string. This can be scanned using the tInput
// interface and acts as a single source string.
class TokenizableIncludeFile : public tInput {
public:
// Copies str, which must be non-empty.
TokenizableString(const TSourceLoc& startLoc, const std::string& str, TPpContext* pp)
// Copies prologue and epilogue. The includedFile must remain valid
// until this TokenizableIncludeFile is no longer used.
TokenizableIncludeFile(const TSourceLoc& startLoc,
const std::string& prologue,
TShader::Includer::IncludeResult* includedFile,
const std::string& epilogue,
TPpContext* pp)
: tInput(pp),
str_(str),
strings(str_.data()),
length(str_.size()),
scanner(1, &strings, &length),
prologue_(prologue),
includedFile_(includedFile),
epilogue_(epilogue),
strings({prologue_.data(), includedFile_->file_data, epilogue_.data()}),
lengths({prologue_.size(), includedFile_->file_length, epilogue_.size()}),
names({
includedFile_->file_name.c_str(),
includedFile_->file_name.c_str(),
includedFile_->file_name.c_str()
}),
scanner(3, strings, lengths, names, 0, 0, true),
prevScanner(nullptr),
stringInput(pp, scanner) {
scanner.setLine(startLoc.line);
@ -456,16 +471,34 @@ protected:
{
prevScanner = pp->parseContext.getScanner();
pp->parseContext.setScanner(&scanner);
pp->push_include(includedFile_);
}
void notifyDeleted() override
{
pp->parseContext.setScanner(prevScanner);
pp->pop_include();
}
void notifyDeleted() override { pp->parseContext.setScanner(prevScanner); }
private:
// Stores the titular string.
const std::string str_;
// Will point to str_[0] and be passed to scanner constructor.
const char* const strings;
// Stores the prologue for this string.
const std::string prologue_;
// Stores the epilogue for this string.
const std::string epilogue_;
// Points to the IncludeResult that this TokenizableIncludeFile represents.
TShader::Includer::IncludeResult* includedFile_;
// Will point to prologue_, includedFile_->file_data and epilogue_
// This is passed to scanner constructor.
// These do not own the storage and it must remain valid until this
// object has been destroyed.
const char* strings[3];
// Length of str_, passed to scanner constructor.
size_t length;
size_t lengths[3];
// String names
const char* names[3];
// Scans over str_.
TInputScanner scanner;
// The previous effective scanner before the scanner in this instance
@ -480,6 +513,24 @@ protected:
void missingEndifCheck();
int lFloatConst(int len, int ch, TPpToken* ppToken);
void push_include(TShader::Includer::IncludeResult* result)
{
currentSourceFile = result->file_name;
includeStack.push(result);
}
void pop_include()
{
TShader::Includer::IncludeResult* include = includeStack.top();
includeStack.pop();
includer.releaseInclude(include);
if (includeStack.empty()) {
currentSourceFile = rootFileName;
} else {
currentSourceFile = includeStack.top()->file_name;
}
}
bool inComment;
//
@ -487,8 +538,12 @@ protected:
//
typedef TUnorderedMap<TString, int> TAtomMap;
typedef TVector<const TString*> TStringMap;
TAtomMap atomMap;
TStringMap stringMap;
std::stack<TShader::Includer::IncludeResult*> includeStack;
std::string currentSourceFile;
std::string rootFileName;
int nextAtom;
void InitAtomTable();
void AddAtomFixed(const char* s, int atom);