Skip to content

Commit 5957d23

Browse files
committed
Fail gracefully and continue parsing when using 'this' keyword.
1 parent bc35ad3 commit 5957d23

5 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/liboslcomp/ast.cpp

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

325325

326326

327-
void
327+
bool
328328
ASTnamed_symbol::check_reserved (ustring name, OSLCompilerImpl *comp)
329329
{
330330
if (Strutil::starts_with(name, "___")) {
331331
comp->error (comp->filename(), comp->lineno(),
332332
"'%s' : sorry, can't start with three underscores",
333333
name);
334+
return false;
335+
} else if (name == "this") {
336+
comp->error (comp->filename(), comp->lineno(),
337+
"'this' not allowed in this context");
338+
return false;
334339
}
340+
return true;
335341
}
336342

337343

@@ -597,6 +603,9 @@ ASTvariable_declaration::ASTvariable_declaration (OSLCompilerImpl *comp,
597603
m_isparam(isparam), m_isoutput(isoutput), m_ismetadata(ismeta),
598604
m_initlist(initlist)
599605
{
606+
if (! check_reserved ())
607+
return;
608+
600609
if (m_initlist && init) {
601610
// Typecheck the init list early.
602611
ASSERT (init->nodetype() == compound_initializer_node);
@@ -606,8 +615,6 @@ ASTvariable_declaration::ASTvariable_declaration (OSLCompilerImpl *comp,
606615
m_typespec = type;
607616
if (! m_ismetadata)
608617
validate (warn_function_clash);
609-
else
610-
check_reserved ();
611618

612619
SymType symtype = isparam ? (isoutput ? SymTypeOutputParam : SymTypeParam)
613620
: 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
@@ -11,4 +11,18 @@ test.osl:21: error: '___threeunderscores' : sorry, can't start with three unders
1111
test.osl:23: error: '___invalid' : sorry, can't start with three underscores
1212
test.osl:24: error: '___invalidfield' : sorry, can't start with three underscores
1313
test.osl:25: error: '___invalidarray' : sorry, can't start with three underscores
14+
test.osl:29: error: 'this' not allowed in this context
15+
test.osl:34: error: 'this' not allowed in this context
16+
test.osl:35: error: 'this' not allowed in this context
17+
test.osl:35: error: 'this' was not declared in this scope
18+
test.osl:36: error: 'this' not allowed in this context
19+
test.osl:36: error: 'this' was not declared in this scope
20+
test.osl:36: error: type 'unknown' does not have a member 'obj'
21+
test.osl:37: error: 'this' not allowed in this context
22+
test.osl:38: error: 'this' not allowed in this context
23+
test.osl:40: error: 'this' not allowed in this context
24+
test.osl:41: error: 'this' not allowed in this context
25+
test.osl:42: error: 'this' not allowed in this context
26+
test.osl:43: error: 'this' not allowed in this context
27+
test.osl:45: error: 'this' not allowed in this context
1428
FAILED test.osl

testsuite/oslc-err-names/test.osl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ struct ___invalid {
2525
float ___invalidarray[3];
2626
};
2727

28-
shader test ()
28+
struct sthis {
29+
float this;
30+
};
31+
32+
struct A { float f; };
33+
34+
void this(float this) {
35+
this;
36+
this.obj;
37+
float this;
38+
float this[5];
39+
40+
float this = 5;
41+
float this[2] = { 1, 1 };
42+
A this = { 5 };
43+
}
44+
45+
shader test (output int this = 5)
2946
{
3047
}
48+

0 commit comments

Comments
 (0)