Skip to content

Commit 90915bd

Browse files
author
Andrin Meier
committed
fix name evaluation bugs (#1132)
Fixed: 1. Type suffix should be ignored when looking up name 2. If name isn't defined, should return Empty
1 parent 67510c1 commit 90915bd

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

Rubberduck.Parsing/Preprocessing/SymbolTable.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public void Add(string name, object value)
1616
_table[name] = value;
1717
}
1818

19+
public bool HasSymbol(string name)
20+
{
21+
return _table.ContainsKey(name);
22+
}
23+
1924
public object Get(string name)
2025
{
2126
if (_table.ContainsKey(name))

Rubberduck.Parsing/Preprocessing/VBAExpressionEvaluator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ private object Evaluate(VBAConditionalCompilationParser.CcExpressionContext expr
6161

6262
private object Visit(VBAConditionalCompilationParser.NameContext context)
6363
{
64-
var identifier = context.GetText();
64+
var identifier = context.IDENTIFIER().GetText();
65+
// Special case, identifier that does not exist is VBAEmpty.
66+
// Could add them to the symbol table, but since they are all constants
67+
// they never change anyway.
68+
if (!_symbolTable.HasSymbol(identifier))
69+
{
70+
return VBAEmptyValue.Value;
71+
}
6572
return _symbolTable.Get(identifier);
6673
}
6774

Rubberduck.Parsing/Preprocessing/VBAPreprocessorVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public override object VisitCcBlock([NotNull] VBAConditionalCompilationParser.Cc
5353
public override object VisitCcConst([NotNull] VBAConditionalCompilationParser.CcConstContext context)
5454
{
5555
// 3.4.1: If <cc-var-lhs> is a <TYPED-NAME> with a <type-suffix>, the <type-suffix> is ignored.
56-
var name = context.ccVarLhs().GetText();
57-
_evaluator.EvaluateConstant(name, context.ccExpression());
56+
var identifier = context.ccVarLhs().name().IDENTIFIER().GetText();
57+
_evaluator.EvaluateConstant(identifier, context.ccExpression());
5858
return MarkLineAsDead(context.GetText());
5959
}
6060

RubberduckTests/Preprocessing/VBAPreprocessorVisitorTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public void TestName()
1515
string code = @"
1616
#Const a = 5
1717
#Const b = a
18+
#Const c = doesNotExist
19+
#Const d& = 1
20+
#Const e = d%
1821
";
1922
var result = Preprocess(code);
2023
Assert.AreEqual(result.Item1.Get("b"), result.Item1.Get("a"));
24+
Assert.AreEqual(VBAEmptyValue.Value, result.Item1.Get("c"));
25+
Assert.AreEqual(1m, result.Item1.Get("d"));
26+
Assert.AreEqual(1m, result.Item1.Get("e"));
2127
}
2228

2329
[TestMethod]

0 commit comments

Comments
 (0)