Skip to content

Commit 4f205f0

Browse files
committed
Fail gracefully and continue parsing when using 'this' keyword.
1 parent e0df57d commit 4f205f0

5 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/liboslcomp/ast.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,20 @@ ASTnamed_symbol::ASTnamed_symbol (NodeType node_type, OSLCompilerImpl *comp,
284284

285285

286286

287-
void
287+
bool
288288
ASTnamed_symbol::check_reserved (ustring name, OSLCompilerImpl *comp)
289289
{
290290
if (Strutil::starts_with(name, "___")) {
291291
comp->error (comp->filename(), comp->lineno(),
292292
"'%s' : sorry, can't start with three underscores",
293293
name);
294+
return false;
295+
} else if (name == "this") {
296+
comp->error (comp->filename(), comp->lineno(),
297+
"'this' not allowed in this context");
298+
return false;
294299
}
300+
return true;
295301
}
296302

297303

@@ -531,11 +537,12 @@ ASTvariable_declaration::ASTvariable_declaration (OSLCompilerImpl *comp,
531537
m_isparam(isparam), m_isoutput(isoutput), m_ismetadata(ismeta),
532538
m_initlist(initlist)
533539
{
540+
if (! check_reserved ())
541+
return;
542+
534543
m_typespec = type;
535544
if (! m_ismetadata)
536545
validate (warn_function_clash);
537-
else
538-
check_reserved ();
539546

540547
SymType symtype = isparam ? (isoutput ? SymTypeOutputParam : SymTypeParam)
541548
: SymTypeLocal;

src/liboslcomp/ast.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,16 @@ class ASTnamed_symbol : public ASTNode
423423
};
424424

425425
/// Validate that m_name is a legal name.
426-
static void check_reserved (ustring name, OSLCompilerImpl *comp);
426+
static bool check_reserved (ustring name, OSLCompilerImpl *comp);
427427

428428
/// Validate that m_name is a legal name, and doesn't conflict with rules
429429
/// given in vflags allowing duplicate symbol if its SymType matches allowed.
430430
static Symbol* validate (ustring name, OSLCompilerImpl *comp,
431431
Validation vflags, int allowed = -1);
432432

433433
protected:
434-
void check_reserved () {
435-
ASTnamed_symbol::check_reserved (m_name, m_compiler);
434+
bool check_reserved () {
435+
return ASTnamed_symbol::check_reserved (m_name, m_compiler);
436436
}
437437

438438
Symbol* validate (Validation vflags, int allowed = -1) {

src/liboslcomp/osllex.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void preprocess (const char *yytext);
179179
"bool"|"case"|"char"|"class"|"const"|"default"|"double" | \
180180
"enum"|"extern"|"false"|"friend"|"inline"|"long"|"private" | \
181181
"protected"|"short"|"signed"|"sizeof"|"static"|"struct" | \
182-
"switch"|"template"|"this"|"true"|"typedef"|"uniform" | \
182+
"switch"|"template"|"true"|"typedef"|"uniform" | \
183183
"union"|"unsigned"|"varying"|"virtual" {
184184
oslcompiler->error (oslcompiler->filename(),
185185
oslcompiler->lineno(),

testsuite/oslc-err-names/ref/out.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@ test.osl:35: error: redefinition of function: int redclfunc (int).
1818
test.osl:40: error: redefinition of builtin function: string concat (string, string).
1919
test.osl:43: error: redefinition of function: void redclfunc ().
2020
previous declaration was at test.osl:42
21+
test.osl:46: error: 'this' not allowed in this context
22+
test.osl:51: error: 'this' not allowed in this context
23+
test.osl:52: error: 'this' not allowed in this context
24+
test.osl:52: error: 'this' was not declared in this scope
25+
test.osl:53: error: 'this' not allowed in this context
26+
test.osl:53: error: 'this' was not declared in this scope
27+
test.osl:53: error: type 'unknown' does not have a member 'obj'
28+
test.osl:54: error: 'this' not allowed in this context
29+
test.osl:55: error: 'this' not allowed in this context
30+
test.osl:57: error: 'this' not allowed in this context
31+
test.osl:58: error: 'this' not allowed in this context
32+
test.osl:59: error: 'this' not allowed in this context
33+
test.osl:60: error: 'this' not allowed in this context
34+
test.osl:62: error: 'this' not allowed in this context
2135
FAILED test.osl

testsuite/oslc-err-names/test.osl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,29 @@ float redclfunc (float f) { return 0.0; } // ok
3939

4040
string concat (string a, string b) { return ""; } // fail
4141

42-
void redclfunc () { int a = 10; }
43-
void redclfunc () { int a = 10; }
42+
void redclfunc () { int a = 10; } // ok
43+
void redclfunc () { int a = 10; } // fail
4444

45-
shader test ()
45+
struct sthis {
46+
float this;
47+
};
48+
49+
struct A { float f; };
50+
51+
void this(float this) {
52+
this;
53+
this.obj;
54+
float this;
55+
float this[5];
56+
57+
float this = 5;
58+
float this[2] = { 1, 1 };
59+
A this = { 5 };
60+
}
61+
62+
shader test (output int this = 5)
4663
{
4764
redclfunc(5);
4865
concat("a", "b");
4966
}
67+

0 commit comments

Comments
 (0)