• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rieske / trans / 29858613021

21 Jul 2026 06:45PM UTC coverage: 90.602% (-0.1%) from 90.735%
29858613021

Pull #52

github

rieske
Fuzz test and fix issues
Pull Request #52: Fuzz test and fix issues

54 of 72 new or added lines in 7 files covered. (75.0%)

100 existing lines in 4 files now uncovered.

5312 of 5863 relevant lines covered (90.6%)

365676.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.48
/src/semantic_analyzer/SymbolTable.cpp
1
#include "SymbolTable.h"
2
#include "util/Logger.h"
3
#include "util/LogManager.h"
4

5
namespace {
6

7
static Logger& out = LogManager::getOutputLogger();
8

9
const std::string LABEL_PREFIX = "__L";
10
unsigned nextLabel { 0 };
11

12
const std::string CONSTANT_PREFIX = "$c";
13
unsigned nextConstant { 0 };
14

15
std::string generateLabelName() {
632✔
16
    return LABEL_PREFIX + std::to_string(++nextLabel);
632✔
17
}
18

19
std::string generateConstantName() {
824✔
20
    return CONSTANT_PREFIX + std::to_string(++nextConstant);
824✔
21
}
22

23
} // namespace
24

25
namespace semantic_analyzer {
26

27
const std::string SymbolTable::SCOPE_PREFIX = "$s";
28

29
bool SymbolTable::insertSymbol(std::string name, const type::Type& type, translation_unit::Context context) {
660✔
30
    if (isAtFileScope()) {
660✔
31
        return globalScope.insertSymbol(name, type, context, true);
114✔
32
    }
33
    return functionScopes.back().insertSymbol(scopePrefix(currentScopeId()) + name, type, context);
546✔
34
}
35

36
std::string SymbolTable::newConstant(const std::string& value) {
824✔
37
    std::string constantSymbol = generateConstantName();
824✔
38
    constants.insert({constantSymbol, value});
824✔
39
    return constantSymbol;
824✔
40
}
×
41

42
void SymbolTable::insertFunctionArgument(std::string name, type::Type type, translation_unit::Context context) {
174✔
43
    functionScopes.back().insertFunctionArgument(scopePrefix(currentScopeId()) + name, type, context);
174✔
44
}
174✔
45

46
FunctionEntry SymbolTable::insertFunction(std::string name, type::Function functionType, translation_unit::Context context) {
1,318✔
47
    FunctionEntry function = functions.insert(std::make_pair(name, FunctionEntry { name, functionType, context })).first->second;
1,318✔
48
    globalScope.insertSymbol(function.getName(), type::function(functionType.getReturnType(), functionType.getArguments()), function.getContext());
1,318✔
49
    return function;
1,318✔
50
}
×
51

52
FunctionEntry SymbolTable::findFunction(std::string name) const {
1,938✔
53
    return functions.at(name);
1,938✔
54
}
55

56
bool SymbolTable::hasFunction(const std::string& name) const {
1,554✔
57
    return functions.find(name) != functions.end();
1,554✔
58
}
59

60
bool SymbolTable::isAtFileScope() const {
1,362✔
61
    return scopeIdStack.empty();
1,362✔
62
}
63

64
bool SymbolTable::hasGlobalVariable(const std::string& name) const {
504✔
65
    try {
66
        return !globalScope.lookup(name).getType().isFunction();
1,006✔
67
    } catch (std::out_of_range&) {
502✔
68
        return false;
502✔
69
    }
502✔
70
}
71

72
void SymbolTable::setGlobalInitializer(const std::string& name, long constantValue) {
28✔
73
    globalScope.setConstantInitializer(name, constantValue);
28✔
74
}
28✔
75

76
bool SymbolTable::hasSymbol(std::string symbolName) const {
2,770✔
77
    try {
78
        lookup(symbolName);
2,780✔
79
        return true;
2,760✔
80
    } catch (std::out_of_range&) {
10✔
81
        return false;
10✔
82
    }
10✔
83
}
84

85
ValueEntry SymbolTable::lookup(std::string name) const {
6,190✔
86
    if (!functionScopes.empty()) {
6,190✔
87
        for (auto it = scopeIdStack.rbegin(); it != scopeIdStack.rend(); ++it) {
9,000✔
88
            try {
89
                return functionScopes.back().lookup(scopePrefix(*it) + name);
12,450✔
90
            } catch (std::out_of_range&) {
2,924✔
91
            }
2,924✔
92
        }
93
    }
94
    return globalScope.lookup(name);
2,512✔
95
}
96

97
ValueEntry SymbolTable::createTemporarySymbol(type::Type type) {
4,076✔
98
    if (functionScopes.empty()) {
4,076✔
99
        return globalScope.createTemporarySymbol(type);
76✔
100
    }
101
    return functionScopes.back().createTemporarySymbol(type);
4,000✔
102
}
103

104
LabelEntry SymbolTable::newLabel() {
632✔
105
    std::string labelName = generateLabelName();
632✔
106
    LabelEntry label { labelName };
632✔
107
    labels.insert(std::make_pair(labelName, label));
632✔
108
    return label;
1,264✔
109
}
632✔
110

111
void SymbolTable::startFunction(std::string name, std::vector<std::string> formalArguments) {
502✔
112
    functionScopes.push_back(ValueScope { });
502✔
113
    scopeIdStack.clear();
502✔
114
    scopeIdStack.push_back(++nextScopeId);
502✔
115
    auto function = findFunction(name);
502✔
116
    size_t i { 0 };
502✔
117
    for (auto& argument : function.arguments()) {
676✔
118
        if (i < formalArguments.size()) {
174✔
119
            insertFunctionArgument(formalArguments.at(i), argument, function.getContext());
174✔
120
        }
121
        ++i;
174✔
122
    }
502✔
123
}
502✔
124

125
void SymbolTable::endFunction() {
502✔
126
    scopeIdStack.clear();
502✔
127
}
502✔
128

129
void SymbolTable::enterBlockScope() {
130✔
130
    scopeIdStack.push_back(++nextScopeId);
130✔
131
}
130✔
132

133
void SymbolTable::exitBlockScope() {
130✔
134
    scopeIdStack.pop_back();
130✔
135
}
130✔
136

137
unsigned SymbolTable::currentScopeId() const {
720✔
138
    return scopeIdStack.back();
720✔
139
}
140

141
std::map<std::string, ValueEntry> SymbolTable::getCurrentScopeSymbols() const {
502✔
142
    return functionScopes.back().getSymbols();
502✔
143
}
144

145
std::vector<ValueEntry> SymbolTable::getCurrentScopeArguments() const {
502✔
146
    return functionScopes.back().getArguments();
502✔
147
}
148

149
std::map<std::string, std::string> SymbolTable::getConstants() const {
386✔
150
    return constants;
386✔
151
}
152

153
std::vector<ValueEntry> SymbolTable::getGlobalVariables() const {
386✔
154
    std::vector<ValueEntry> globals;
386✔
155
    for (const auto& entry : globalScope.getSymbols()) {
1,824✔
156
        if (entry.second.isGlobal()) {
1,438✔
157
            globals.push_back(entry.second);
112✔
158
        }
159
    }
386✔
160
    return globals;
386✔
UNCOV
161
}
×
162

163
void SymbolTable::printTable() const {
386✔
164
    for (auto constant : constants) {
1,198✔
165
        out << "\t" << constant.first << "\t\t\t\t" << constant.second << "\n";
812✔
166
    }
812✔
167
    for (auto function : functions) {
1,636✔
168
        out << "\t" << function.first << "\t\t\t\t" << function.second.getType().to_string() << "\n";
1,250✔
169
    }
1,250✔
170
    for (auto label : labels) {
1,014✔
171
        out << "\t" << label.second.getName() << "\t\ttemp\t0\t\tlabel\n";
628✔
172
    }
628✔
173
    for (unsigned i = 0; i < functionScopes.size(); i++) {
864✔
174
        out << "BEGIN SCOPE\n"
478✔
175
            << "--arguments--\n";
478✔
176
        for (const auto& value : functionScopes[i].getArguments()) {
648✔
177
            out << value.to_string();
170✔
178
        }
478✔
179
        out << "--locals--\n";
478✔
180
        for (const auto& value : functionScopes[i].getSymbols()) {
4,940✔
181
            out << value.second.to_string();
4,462✔
182
        }
478✔
183
        out << "END SCOPE\n";
478✔
184
    }
185
}
386✔
186

187
std::string SymbolTable::scopePrefix(unsigned scopeId) const {
7,322✔
188
    return SCOPE_PREFIX + std::to_string(scopeId);
7,322✔
189
}
190

191
} // namespace semantic_analyzer
192

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc