Skip to content

Commit

Permalink
Should see predefined symbols as macros (#887)
Browse files Browse the repository at this point in the history
Change command-line defines to actually use defines.
  • Loading branch information
PerfectLaugh authored Aug 20, 2023
1 parent 9ce936d commit 4328449
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion compiler/compile-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ struct CompileOptions {
bool show_includes = false;
bool syntax_only = false;
int verbosity = 1; /* verbosity level, 0=quiet, 1=normal, 2=verbose */
std::vector<std::pair<std::string, int>> constants;
std::vector<std::pair<std::string, std::string>> predefines;
};
7 changes: 3 additions & 4 deletions compiler/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ static void parseoptions(CompileContext& cc, int argc, char** argv) {
parser.allow_slashes();
}

parser.add_usage_line("sym=val", "Define constant \"sym\" with value \"val\".");
parser.add_usage_line("sym=", "Define constant \"sym\" with value 0.");
parser.add_usage_line("sym=val", "Define macro \"sym\" with value \"val\".");
parser.add_usage_line("sym=", "Define macro \"sym\" with value 0.");

auto usage = "[options] <filename> [filename...]";
parser.set_usage_line(usage);
Expand Down Expand Up @@ -205,8 +205,7 @@ static void parseoptions(CompileContext& cc, int argc, char** argv) {
} else if ((ptr = strchr(arg, '=')) != NULL) {
int i = (int)(ptr - arg);
SafeStrcpyN(str, PATH_MAX, arg, i);
i = atoi(ptr + 1);
cc.options()->constants.emplace_back(str, i);
cc.options()->predefines.emplace_back(str, ptr + 1);
} else {
std::string path = arg;
set_extension(&path, ".sp", false);
Expand Down
8 changes: 4 additions & 4 deletions compiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ int RunCompiler(int argc, char** argv, CompileContext& cc) {

auto options = cc.options();

for (const auto& pair : options->constants) {
DefineConstant(cc, cc.atom(pair.first), pair.second, sGLOBAL);
}

#ifdef __EMSCRIPTEN__
setup_emscripten_fs();
#endif
Expand All @@ -147,6 +143,10 @@ int RunCompiler(int argc, char** argv, CompileContext& cc) {

cc.lexer()->Init(cc.inpf_org());

for (const auto& pair : options->predefines) {
cc.lexer().get()->AddMacro(pair.first.c_str(), pair.second.c_str());
}

setconstants(); /* set predefined constants and tagnames */

inst_datetime_defines(cc);
Expand Down
7 changes: 7 additions & 0 deletions tests/compile-only/defines/fail-main.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#if defined A
#define B 20
#endif

public int DefinesFailTest() {
return A + B;
}
2 changes: 2 additions & 0 deletions tests/compile-only/defines/fail-main.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(6) : error 017: undefined symbol "A"
(6) : error 017: undefined symbol "B"
9 changes: 9 additions & 0 deletions tests/compile-only/defines/ok-main.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// defines: ["A=10"]

#if defined A
#define B 20
#endif

public int DefinesFailTest() {
return A + B;
}
32 changes: 27 additions & 5 deletions tests/runtests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# vim: set ts=2 sw=2 tw=99 et:
import argparse
import ast
import datetime
import os
import platform
import re
import subprocess
import sys

import testutil
from testutil import manifest_get

Expand Down Expand Up @@ -252,12 +253,13 @@ def find_tests_impl(self, local_folder, manifest):
###
class Test(object):
ManifestKeys = set([
'type',
'returnCode',
'warnings_are_errors',
'compiler',
'force_old_parser',
'defines',
'force_new_parser',
'force_old_parser',
'returnCode',
'type',
'warnings_are_errors',
])

def __init__(self, **kwargs):
Expand Down Expand Up @@ -341,6 +343,22 @@ def expectedReturnCode(self):
if 'returnCode' in self.local_manifest_:
return int(self.local_manifest_['returnCode'])
return 0

@property
def defines(self):
if 'defines' in self.local_manifest_:
value = self.local_manifest_['defines']
if isinstance(value, str):
try:
value = ast.literal_eval(value)
if isinstance(value, list):
return value
except:
pass

return [value]

return []

def should_run(self, mode):
compiler = self.local_manifest_.get('compiler', None)
Expand Down Expand Up @@ -495,8 +513,12 @@ def run_compiler(self, mode, test):
argv += mode['spcomp']['args']
argv += mode['args']
argv += ['-z', '1'] # Fast compilation for tests.

if test.warnings_are_errors:
argv += ['-E']
for define in test.defines:
argv += [define]

if mode['spcomp']['name'] == 'spcomp2':
argv += ['-o', test.smx_path]
argv += [self.fix_path(spcomp_path, test.path)]
Expand Down

0 comments on commit 4328449

Please sign in to comment.