Skip to content

Commit 972ff96

Browse files
committed
Preserve define variables and comma conditionals
Finding: [P2] Preserve variables declared with define — /Users/tomasz/github/t-kalinowski/quickr/R/aaa-utils.R:282-284 Resolution: define blocks now assign their collected body to the declared make variable while still ignoring body lines as standalone scanner input. Added a regression test for a define variable used by include. Finding: [P2] Split ifeq operands at the first comma — /Users/tomasz/github/t-kalinowski/quickr/R/aaa-utils.R:375-377 Resolution: parenthesized ifeq and ifneq parsing now uses the first comma as the operand separator. Added a regression test for a comma-containing right operand.
1 parent c08867e commit 972ff96

2 files changed

Lines changed: 106 additions & 10 deletions

File tree

R/aaa-utils.R

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ quickr_makevars_scan_one_include_path <- function(path, variables, visited) {
246246
out <- character()
247247
vars <- variables
248248
conditional_stack <- list()
249-
in_define <- FALSE
249+
define_assignment <- NULL
250+
define_lines <- character()
250251

251252
for (line in lines) {
252-
if (startsWith(line, "\t") && !in_define) {
253+
if (startsWith(line, "\t") && is.null(define_assignment)) {
253254
next
254255
}
255256

@@ -258,9 +259,14 @@ quickr_makevars_scan_one_include_path <- function(path, variables, visited) {
258259
next
259260
}
260261

261-
if (in_define) {
262+
if (!is.null(define_assignment)) {
262263
if (quickr_makevars_define_end(line)) {
263-
in_define <- FALSE
264+
define_assignment$value <- paste(define_lines, collapse = "\n")
265+
vars <- quickr_makevars_apply_assignment(vars, define_assignment)
266+
define_assignment <- NULL
267+
define_lines <- character()
268+
} else {
269+
define_lines <- c(define_lines, line)
264270
}
265271
next
266272
}
@@ -279,8 +285,9 @@ quickr_makevars_scan_one_include_path <- function(path, variables, visited) {
279285
next
280286
}
281287

282-
if (quickr_makevars_define_start(line)) {
283-
in_define <- TRUE
288+
define_assignment <- quickr_makevars_define_assignment(line)
289+
if (!is.null(define_assignment)) {
290+
define_lines <- character()
284291
next
285292
}
286293

@@ -306,12 +313,23 @@ quickr_makevars_scan_one_include_path <- function(path, variables, visited) {
306313
list(paths = out, variables = vars)
307314
}
308315

309-
quickr_makevars_define_start <- function(line) {
310-
grepl(
311-
"^(?:(?:export|override)[[:space:]]+)*define(?:[[:space:]]|$)",
316+
quickr_makevars_define_assignment <- function(line) {
317+
match <- regexec(
318+
"^((?:(?:export|override)[[:space:]]+)*)?define[[:space:]]+([A-Za-z_][A-Za-z0-9_.-]*).*$",
312319
line,
313320
perl = TRUE
314321
)
322+
parts <- regmatches(line, match)[[1]]
323+
if (length(parts) != 3L) {
324+
return(NULL)
325+
}
326+
327+
list(
328+
name = parts[[3]],
329+
operator = "=",
330+
value = "",
331+
override = grepl("(^|[[:space:]])override[[:space:]]+", parts[[2]])
332+
)
315333
}
316334

317335
quickr_makevars_define_end <- function(line) {
@@ -373,7 +391,7 @@ quickr_makevars_conditionals_active <- function(stack) {
373391

374392
quickr_makevars_condition_result <- function(line, variables) {
375393
match <- regexec(
376-
"^(ifeq|ifneq)[[:space:]]*\\((.*),(.*)\\)$",
394+
"^(ifeq|ifneq)[[:space:]]*\\((.*?),(.*)\\)$",
377395
line,
378396
perl = TRUE
379397
)

tests/testthat/test-compiler-cache.R

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,46 @@ test_that("quickr_cached_r_cmd_config_value respects Makevars conditionals", {
890890
expect_equal(calls, 2L)
891891
})
892892

893+
test_that("quickr_cached_r_cmd_config_value splits ifeq at the first comma", {
894+
cache <- new.env(parent = emptyenv())
895+
root <- withr::local_tempdir()
896+
active_root <- file.path(root, "active")
897+
dir.create(active_root)
898+
makevars <- file.path(root, "Makevars")
899+
toolchain <- file.path(active_root, "toolchain.mk")
900+
writeLines(
901+
c(
902+
"TARGET = x86,arm64",
903+
"ifeq ($(TARGET),x86,arm64)",
904+
paste("include", toolchain),
905+
"endif"
906+
),
907+
makevars
908+
)
909+
writeLines("FC=gfortran", toolchain)
910+
calls <- 0L
911+
local_mocked_bindings(
912+
quickr_r_cmd_config_probe = function(name) {
913+
calls <<- calls + 1L
914+
list(value = paste0("value-", calls), ok = TRUE)
915+
},
916+
.package = "quickr"
917+
)
918+
withr::local_envvar(R_MAKEVARS_USER = makevars)
919+
920+
expect_identical(
921+
quickr_cached_r_cmd_config_value("FC", cache = cache),
922+
"value-1"
923+
)
924+
925+
writeLines("FC=flang", toolchain)
926+
expect_identical(
927+
quickr_cached_r_cmd_config_value("FC", cache = cache),
928+
"value-2"
929+
)
930+
expect_equal(calls, 2L)
931+
})
932+
893933
test_that("quickr_cached_r_cmd_config_value respects quoted Makevars conditionals", {
894934
cache <- new.env(parent = emptyenv())
895935
root <- withr::local_tempdir()
@@ -1680,6 +1720,44 @@ test_that("quickr_cached_r_cmd_config_value ignores Makevars define bodies", {
16801720
expect_equal(calls, 2L)
16811721
})
16821722

1723+
test_that("quickr_cached_r_cmd_config_value preserves Makevars define variables", {
1724+
cache <- new.env(parent = emptyenv())
1725+
root <- withr::local_tempdir()
1726+
makevars <- file.path(root, "Makevars")
1727+
toolchain <- file.path(root, "toolchain.mk")
1728+
writeLines(
1729+
c(
1730+
"define TOOLCHAIN",
1731+
toolchain,
1732+
"endef",
1733+
"include $(TOOLCHAIN)"
1734+
),
1735+
makevars
1736+
)
1737+
writeLines("FC=gfortran", toolchain)
1738+
calls <- 0L
1739+
local_mocked_bindings(
1740+
quickr_r_cmd_config_probe = function(name) {
1741+
calls <<- calls + 1L
1742+
list(value = paste0("value-", calls), ok = TRUE)
1743+
},
1744+
.package = "quickr"
1745+
)
1746+
withr::local_envvar(R_MAKEVARS_USER = makevars)
1747+
1748+
expect_identical(
1749+
quickr_cached_r_cmd_config_value("FC", cache = cache),
1750+
"value-1"
1751+
)
1752+
1753+
writeLines("FC=flang", toolchain)
1754+
expect_identical(
1755+
quickr_cached_r_cmd_config_value("FC", cache = cache),
1756+
"value-2"
1757+
)
1758+
expect_equal(calls, 2L)
1759+
})
1760+
16831761
test_that("quickr_cached_r_cmd_config_value preserves command-line VAR", {
16841762
cache <- new.env(parent = emptyenv())
16851763
root <- withr::local_tempdir()

0 commit comments

Comments
 (0)