Skip to content

Commit

Permalink
[C++] Honor "override" when setting global variable
Browse files Browse the repository at this point in the history
Regression when compared to GNU make behaviour.

Test case:

 $ cat Makefile.override-failure
 $(info VAR: '$(VAR)')
 override VAR := test
 $(info VAR: '$(VAR)')
 override VAR := test-new
 $(info VAR: '$(VAR)')
 VAR := test-should-not-work
 $(info VAR: '$(VAR)')

 $ make -f Makefile.override-failure
 VAR: ''
 VAR: 'test'
 VAR: 'test-new'
 VAR: 'test-new'
 make: *** No targets.  Stop.

 $ ckati -c --warn -f Makefile.override-failure
 VAR: ''
 VAR: 'test'
 VAR: 'test'
 VAR: 'test'
 *** No targets.

Fixes google/kati#50

Change-Id: I9c4185c30cfcf5602da7e0ac98b7e9c420788005
  • Loading branch information
Stefan Becker committed Apr 12, 2016
1 parent 167e1f7 commit 29b9b74
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void Evaluator::EvalAssign(const AssignStmt* stmt) {
Var* rhs = EvalRHS(lhs, stmt->rhs, stmt->orig_rhs, stmt->op,
stmt->directive == AssignDirective::OVERRIDE);
if (rhs)
lhs.SetGlobalVar(rhs);
lhs.SetGlobalVar(rhs,
stmt->directive == AssignDirective::OVERRIDE);
}

void Evaluator::EvalRule(const RuleStmt* stmt) {
Expand Down
9 changes: 6 additions & 3 deletions symtab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "symtab.h"

#include <iostream>

#ifdef ENABLE_TID_CHECK
#include <pthread.h>
#endif
Expand Down Expand Up @@ -59,13 +61,14 @@ Var* Symbol::GetGlobalVar() const {
return v;
}

void Symbol::SetGlobalVar(Var* v) const {
void Symbol::SetGlobalVar(Var* v, bool is_override) const {
if (static_cast<size_t>(v_) >= g_symbol_data.size()) {
g_symbol_data.resize(v_ + 1);
}
Var* orig = g_symbol_data[v_].gv;
if (orig->Origin() == VarOrigin::OVERRIDE ||
orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) {
if (!is_override &&
(orig->Origin() == VarOrigin::OVERRIDE ||
orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE)) {
return;
}
if (orig->Origin() == VarOrigin::AUTOMATIC) {
Expand Down
2 changes: 1 addition & 1 deletion symtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Symbol {
bool IsValid() const { return v_ >= 0; }

Var* GetGlobalVar() const;
void SetGlobalVar(Var* v) const;
void SetGlobalVar(Var* v, bool is_override = false) const;

private:
explicit Symbol(int v);
Expand Down

0 comments on commit 29b9b74

Please sign in to comment.