Skip to content

Commit 195330f

Browse files
committed
Enable Wave pragma once support
1 parent fdea055 commit 195330f

3 files changed

Lines changed: 42 additions & 37 deletions

File tree

3rdparty/boost/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ endforeach()
120120
# include will lead to ABI mismatch hence we update the target and let inherit options
121121
target_compile_definitions(boost_wave
122122
PUBLIC BOOST_WAVE_ENABLE_COMMANDLINE_MACROS=1
123-
PUBLIC BOOST_WAVE_SUPPORT_PRAGMA_ONCE=0
123+
PUBLIC BOOST_WAVE_SUPPORT_PRAGMA_ONCE=1
124124
PUBLIC BOOST_WAVE_EMIT_PRAGMA_DIRECTIVES=1
125125
PUBLIC BOOST_WAVE_SERIALIZATION=0
126126
PUBLIC BOOST_WAVE_SUPPORT_INCLUDE_NEXT=0
@@ -131,4 +131,4 @@ target_compile_definitions(boost_wave
131131

132132
set(NBL_BOOST_TARGETS
133133
${NBL_BOOST_TARGETS}
134-
PARENT_SCOPE)
134+
PARENT_SCOPE)

src/nbl/asset/utils/waveContext.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <boost/wave.hpp>
99
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
1010
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
11+
#include <unordered_set>
1112

1213
#include "nbl/asset/utils/IShaderCompiler.h"
1314

@@ -191,6 +192,7 @@ class context : private boost::noncopyable
191192
context(target_iterator_type const& first_, target_iterator_type const& last_, char const* fname, preprocessing_hooks const& hooks_)
192193
: first(first_), last(last_), filename(fname)
193194
, has_been_initialized(false)
195+
, current_filename(fname)
194196
, current_relative_filename(fname)
195197
, macros(*this_())
196198
, language(language_support(
@@ -440,6 +442,32 @@ class context : private boost::noncopyable
440442
}
441443

442444
public:
445+
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
446+
void set_current_filename(char const* real_name)
447+
{
448+
current_filename = real_name;
449+
}
450+
std::string const& get_current_filename() const
451+
{
452+
return current_filename;
453+
}
454+
455+
bool has_pragma_once(std::string const& filename_) const
456+
{
457+
return pragma_once_headers.contains(filename_);
458+
}
459+
bool add_pragma_once_header(std::string const& filename_, std::string const& guard_name)
460+
{
461+
get_hooks().detected_include_guard(derived(), filename_, guard_name);
462+
return pragma_once_headers.emplace(filename_).second;
463+
}
464+
bool add_pragma_once_header(token_type const& pragma_, std::string const& filename_)
465+
{
466+
get_hooks().detected_pragma_once(derived(), pragma_, filename_);
467+
return pragma_once_headers.emplace(filename_).second;
468+
}
469+
#endif
470+
443471
void set_current_relative_filename(char const* real_name)
444472
{
445473
current_relative_filename = real_name;
@@ -464,12 +492,16 @@ class context : private boost::noncopyable
464492
const std::string filename; // associated main filename
465493
bool has_been_initialized; // set cwd once
466494

495+
std::string current_filename; // real name of current preprocessed file
467496
std::string current_relative_filename; // real relative name of current preprocessed file
468497

469498
// Nabla Additions Start
470499
// these are temporaries!
471500
system::path current_dir;
472501
core::string located_include_content;
502+
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
503+
std::unordered_set<std::string> pragma_once_headers;
504+
#endif
473505
// Cache Additions
474506
bool cachingRequested = false;
475507
std::vector<IShaderCompiler::CCache::SEntry::SPreprocessingDependency> dependencies = {};
@@ -525,6 +557,11 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
525557
return false;
526558
}
527559

560+
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
561+
if (ctx.has_pragma_once(result.absolutePath.string()))
562+
return true;
563+
#endif
564+
528565
// If caching was requested, push a new SDependency onto dependencies
529566
if (ctx.cachingRequested) {
530567
ctx.dependencies.emplace_back(ctx.get_current_directory(), file_path, standardInclude, std::move(result.hash));
@@ -559,6 +596,9 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
559596
must_emit_line_directive = true;
560597

561598
act_pos.set_file(iter_ctx->filename); // initialize file position
599+
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
600+
ctx.set_current_filename(result.absolutePath.string().c_str());
601+
#endif
562602

563603
ctx.set_current_relative_filename(file_path.c_str());
564604
iter_ctx->real_relative_filename = file_path.c_str();

tools/nsc/main.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -436,39 +436,6 @@ class ShaderCompiler final : public IApplicationFramework
436436
return out;
437437
}
438438

439-
static std::string stripStandalonePragmaOnceLines(std::string_view text)
440-
{
441-
std::string out;
442-
out.reserve(text.size());
443-
444-
size_t lineStart = 0u;
445-
while (lineStart < text.size())
446-
{
447-
const auto lineEnd = text.find('\n', lineStart);
448-
const auto lineSize = lineEnd == std::string_view::npos ? text.size() - lineStart : lineEnd - lineStart;
449-
const auto line = text.substr(lineStart, lineSize);
450-
451-
auto trimmed = line;
452-
while (!trimmed.empty() && (trimmed.front() == ' ' || trimmed.front() == '\t'))
453-
trimmed.remove_prefix(1u);
454-
while (!trimmed.empty() && trimmed.back() == '\r')
455-
trimmed.remove_suffix(1u);
456-
457-
const auto isStandalonePragmaOnce = trimmed == "#pragma once";
458-
if (!isStandalonePragmaOnce)
459-
out.append(line.data(), line.size());
460-
461-
if (lineEnd != std::string_view::npos)
462-
out.push_back('\n');
463-
464-
if (lineEnd == std::string_view::npos)
465-
break;
466-
lineStart = lineEnd + 1u;
467-
}
468-
469-
return out;
470-
}
471-
472439
static void dumpBuildInfo(const argparse::ArgumentParser& program)
473440
{
474441
::json j;
@@ -582,8 +549,6 @@ class ShaderCompiler final : public IApplicationFramework
582549
std::string_view code(codePtr, std::strlen(codePtr));
583550

584551
r.text = hlslcompiler->preprocessShader(std::string(code), shaderStage, opt, nullptr);
585-
if (!r.text.empty())
586-
r.text = stripStandalonePragmaOnceLines(r.text);
587552
r.ok = !r.text.empty();
588553
r.view = r.text;
589554
return r;

0 commit comments

Comments
 (0)