Skip to content

Commit

Permalink
Prevent data space redeclaration #19
Browse files Browse the repository at this point in the history
  • Loading branch information
riftEmber committed Nov 23, 2021
1 parent 08e9fd8 commit b8e0810
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/DataAccessHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct DataAccessHandler {
//! Add the space accessed as a write, and any sub-accesses as reads
void processExprAsWrite(Expr *expr);

//! Add a scalar's name as a write DataAccess.
void processWriteToScalarName(const std::string name);

//! Make all data accesses, including subaccesses, from the given expression
//! \param[in] fullExpr Expression to process
//! \param[in] isRead Whether this access is a read
Expand Down
8 changes: 8 additions & 0 deletions src/ComputationBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,16 @@ void ComputationBuilder::addStmt(clang::Stmt *clangStmt) {
DataAccessHandler dataAccesses;
if (auto *asDeclStmt = dyn_cast<DeclStmt>(clangStmt)) {
auto *decl = cast<VarDecl>(asDeclStmt->getSingleDecl());
std::string varName = decl->getNameAsString();
// If this declaration's variable is already registered as a data space, this is another declaration by that name.
if (computation->isDataSpace(varName)) {
Utils::printErrorAndExit(
"Declaring a variable with a name that has already been used in another scope is disallowed",
asDeclStmt);
}
if (decl->hasInit()) {
dataAccesses.processComplexExprAsReads(decl->getInit());
dataAccesses.processWriteToScalarName(varName);
}
} else if (auto *asBinOper = dyn_cast<BinaryOperator>(clangStmt)) {
if (auto *lhsAsArrayAccess =
Expand Down
13 changes: 13 additions & 0 deletions src/ComputationBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,19 @@ TEST_F(ComputationBuilderDeathTest, invalid_condition_fails) {
"Not-equal conditions are unsupported by SPF: in condition x != 0");
}

TEST_F(ComputationBuilderDeathTest, reusing_var_name_fails) {
std::string code =
"int a() {\
int x = 5;\
for (int i = 0; i < 5; i += 1) {\
int x = 3;\
}\
return x;\
}";
ASSERT_DEATH(buildComputationFromCode(code),
"Declaring a variable with a name that has already been used in another scope is disallowed");
}

//! Set up and run tests
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
9 changes: 9 additions & 0 deletions src/DataAccessHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ void DataAccessHandler::processExprAsWrite(Expr *expr) {
processSingleAccessExpr(expr, false);
}

void DataAccessHandler::processWriteToScalarName(const std::string name) {
if (ComputationBuilder::positionContext->isIteratorName(name)) {
return;
}

dataSpacesAccessed.emplace(name);
stmtDataAccesses.push_back(DataAccess(name, 0, false, false, {}));
}

void DataAccessHandler::processSingleAccessExpr(Expr *fullExpr,
bool isRead) {
auto accesses = makeDataAccessesFromExpr(fullExpr, isRead);
Expand Down

0 comments on commit b8e0810

Please sign in to comment.