Skip to content

Commit

Permalink
#pragma osl nowarn suppresses warning on the following source line. (
Browse files Browse the repository at this point in the history
…#864)

This sets the precedent that all future osl pragmas will start with
`#pragma osl`, and that any other token after the `#pragma` will be
ignored.
  • Loading branch information
lgritz committed Feb 16, 2018
1 parent 020214b commit eac82a0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -37,6 +37,10 @@ OSL Language and oslc compiler:
* A shader input parameter marked with metadata `[[ int allowconnect = 0 ]]`
will disallow runtime connections via `ConnectShaders()`, resulting in an
error. #857 (1.10.0)
* oslc command-line argument `-Werror` will treat all warnings as hard
errors (failed compilation). (1.10.0)
* `#pragma nowarn` will suppress any warnings arising from code on the
immediately following line of that source file. (1.10.0)

OSL Standard library:

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -283,6 +283,7 @@ TESTSUITE ( aastep allowconnect-err and-or-not-synonyms arithmetic
oslinfo-metadata oslinfo-noparams
osl-imageio
paramval-floatpromotion
pragma-nowarn
printf-whole-array
raytype raytype-specialized reparam
render-background render-bumptest
Expand Down
21 changes: 17 additions & 4 deletions src/liboslcomp/oslcomp_pvt.h
Expand Up @@ -100,7 +100,7 @@ class OSLCompilerImpl {

/// Error reporting
template<typename... Args>
void error (string_view filename, int line,
void error (ustring filename, int line,
string_view format, const Args&... args) const
{
ASSERT (format.size());
Expand All @@ -116,10 +116,12 @@ class OSLCompilerImpl {

/// Warning reporting
template<typename... Args>
void warning (string_view filename, int line,
void warning (ustring filename, int line,
string_view format, const Args&... args) const
{
ASSERT (format.size());
if (nowarn(filename, line))
return; // skip if the filename/line is on the nowarn list
std::string msg = OIIO::Strutil::format (format, args...);
if (msg.size() && msg.back() == '\n') // trim extra newline
msg.pop_back();
Expand All @@ -135,7 +137,7 @@ class OSLCompilerImpl {

/// Info reporting
template<typename... Args>
void info (string_view filename, int line,
void info (ustring filename, int line,
string_view format, const Args&... args) const
{
ASSERT (format.size());
Expand All @@ -150,7 +152,7 @@ class OSLCompilerImpl {

/// message reporting
template<typename... Args>
void message (string_view filename, int line,
void message (ustring filename, int line,
string_view format, const Args&... args) const
{
ASSERT (format.size());
Expand Down Expand Up @@ -379,6 +381,16 @@ class OSLCompilerImpl {
m_func_decls.emplace_back (f);
}

// Add a pragma nowarn for the following line
void pragma_nowarn () {
m_nowarn_lines.insert ({filename(), lineno()+1});
}

// Is the line amont
bool nowarn (ustring filename, int line) const {
return m_nowarn_lines.find({filename, line}) != m_nowarn_lines.end();
}

private:
void initialize_globals ();
void initialize_builtin_funcs ();
Expand Down Expand Up @@ -477,6 +489,7 @@ class OSLCompilerImpl {
Symbol *m_derivsym; ///< Pseudo-symbol to track deriv dependencies
int m_main_method_start; ///< Instruction where 'main' starts
bool m_declaring_shader_formals; ///< Are we declaring shader formals?
std::set<std::pair<ustring,int>> m_nowarn_lines; ///< Lines for 'nowarn'
};


Expand Down
14 changes: 12 additions & 2 deletions src/liboslcomp/osllex.l
Expand Up @@ -306,8 +306,18 @@ preprocess (const char *yytext)
p++;
if (! strncmp (p, "pragma", 6)) {
// pragma
oslcompiler->error (oslcompiler->filename(), oslcompiler->lineno(),
"Unknown pragma '%s'", p);
OIIO::string_view line (p+6);
string_view pragmatype = OIIO::Strutil::parse_word (line);
if (OIIO::Strutil::iequals (pragmatype, "osl")) {
string_view pragmaname = OIIO::Strutil::parse_word (line);
if (pragmaname == "nowarn") {
oslcompiler->pragma_nowarn ();
} else {
oslcompiler->warning (oslcompiler->filename(), oslcompiler->lineno(),
"Unknown pragma '%s'", pragmaname);
}
}
// N.B. Pragmas that don't start with "osl" are ignored
oslcompiler->incr_lineno(); // the pragma ends with an EOLN
} else { /* probably the line number and filename */
if (! strncmp (p, "line", 4))
Expand Down
2 changes: 2 additions & 0 deletions testsuite/pragma-nowarn/ref/out.txt
@@ -0,0 +1,2 @@
Compiled test.osl -> test.oso

11 changes: 11 additions & 0 deletions testsuite/pragma-nowarn/run.py
@@ -0,0 +1,11 @@
#!/usr/bin/env python

# This shader would ordinarily issue a warning.
# With -Werror, it should be upgraded to an error.
oslcargs = "-Werror"

# BUT... the shader carefully uses #pragma nowarn to disable the warning.
# Which should cause the test to pass.

command = testshade("test")

10 changes: 10 additions & 0 deletions testsuite/pragma-nowarn/test.osl
@@ -0,0 +1,10 @@
// Engineer an ambiguous case that should be a warning

normal func () { return 1; }
vector func () { return 2; }

shader test ()
{
#pragma osl nowarn
point p = func();
}

0 comments on commit eac82a0

Please sign in to comment.