Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oslc: detect using type name as an variable identifier #1457

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ macro (osl_add_all_tests)
oslc-err-outputparamvararray oslc-err-paramdefault
oslc-err-struct-array-init oslc-err-struct-ctr
oslc-err-struct-dup oslc-err-struct-print
oslc-err-type-as-variable
oslc-err-unknown-ctr
oslc-pragma-warnerr
oslc-warn-commainit
Expand Down
3 changes: 2 additions & 1 deletion src/include/osl_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ enum SymType {
SymTypeGlobal,
SymTypeConst,
SymTypeFunction,
SymTypeType
SymTypeType,
SymTypeLast
};


Expand Down
4 changes: 4 additions & 0 deletions src/liboslcomp/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ ASTvariable_ref::ASTvariable_ref(OSLCompilerImpl* comp, ustring name)
errorf("function '%s' can't be used as a variable", name);
return;
}
if (m_sym->symtype() == SymTypeType) {
errorf("type name '%s' can't be used as a variable", name);
return;
}
m_typespec = m_sym->typespec();
}

Expand Down
4 changes: 2 additions & 2 deletions src/liboslcomp/symtab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Symbol::unmangled() const
const char*
Symbol::symtype_shortname(SymType s)
{
OSL_DASSERT((int)s >= 0 && (int)s < (int)SymTypeType);
OSL_DASSERT((int)s >= 0 && (int)s < (int)SymTypeLast);
static const char* names[] = { "param", "oparam", "local", "temp",
"global", "const", "func" };
"global", "const", "func", "typename" };
return names[(int)s];
}

Expand Down
6 changes: 3 additions & 3 deletions testsuite/oslc-err-struct-print/test.osl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test (
output testStruct out = input
)
{
printf("testStruct: %g\n", testStruct.c);
printf("testStruct: %g\n", testStruct);
printf("testStruct: %d\n", testStruct);
printf("testStruct: %g\n", input.c);
printf("testStruct: %g\n", input);
printf("testStruct: %d\n", input);
}
1 change: 1 addition & 0 deletions testsuite/oslc-err-type-as-variable/NOOPTIMIZE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing oslc error detection, no execution, no need to optimize
1 change: 1 addition & 0 deletions testsuite/oslc-err-type-as-variable/NOOPTIX
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing oslc error detection, no execution, no need to run with OptiX
3 changes: 3 additions & 0 deletions testsuite/oslc-err-type-as-variable/ref/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.osl:11: error: type name 'Data' can't be used as a variable
test.osl:11: error: Cannot return a 'unknown' from 'struct Data foo()'
FAILED test.osl
9 changes: 9 additions & 0 deletions testsuite/oslc-err-type-as-variable/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python

# Copyright Contributors to the Open Shading Language project.
# SPDX-License-Identifier: BSD-3-Clause
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

# command = oslc("test.osl")
# don't even need that -- it's automatic
failureok = 1 # this test is expected to have oslc errors
18 changes: 18 additions & 0 deletions testsuite/oslc-err-type-as-variable/test.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

struct Data {
float height;
};

Data foo(Data a, float b)
{
return Data; // Error: type name 'Data' can't be used as a variable
}

shader test ()
{
Data d;
d = foo(d, 1.0);
}