Merge branch 'master' into hlsl-frontend

This commit is contained in:
John Kessenich 2016-04-03 20:36:48 -06:00
commit 1c7e70763b
66 changed files with 3048 additions and 588 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(TParseContextBase& pc, const TShader::Includer& inclr) :
preamble(0), strings(0), parseContext(pc), includer(inclr), inComment(false)
TPpContext::TPpContext(TParseContextBase& 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(TParseContextBase&, const TShader::Includer&);
TPpContext(TParseContextBase&, 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,21 +431,40 @@ 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),
scanner(3, strings, lengths, names, 0, 0, true),
prevScanner(nullptr),
stringInput(pp, scanner) {
scanner.setLine(startLoc.line);
scanner.setString(startLoc.string);
scanner.setFile(startLoc.name);
stringInput(pp, scanner)
{
strings[0] = prologue_.data();
strings[1] = includedFile_->file_data;
strings[2] = epilogue_.data();
lengths[0] = prologue_.size();
lengths[1] = includedFile_->file_length;
lengths[2] = epilogue_.size();
scanner.setLine(startLoc.line);
scanner.setString(startLoc.string);
scanner.setFile(startLoc.name, 0);
scanner.setFile(startLoc.name, 1);
scanner.setFile(startLoc.name, 2);
}
// tInput methods:
@ -456,16 +476,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 +518,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 +543,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);