Clojure: `if`, `when`, `cond`, `case`, `doseq`, `for` and `loop` count as decisions and nesting (cx and nest are 1 and 0 for every Clojure function today)

Aperta
#874 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
clojure, cpp
Ambito
devtools, tooling

Direzione di ricerca

Read cc_walk in src/ingest_metrics.h and clojureListHead in src/ingest_clojure.h, paying attention to the include order noted in the issue. Add the regression fixture and assertions in test/clojurecheck.sh or a registered metrics check, then bump both parser version constants. Done means the expected Clojure metrics and near misses pass, the Elixir checks remain green, and the listed identity and quality delta checks meet their acceptance criteria.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement help wanted

Claim it in this issue first. Size: S-M. Depends on: nothing. Language: Clojure. Siblings: #548 (Clojure call heads) and open PR #747 touch src/ingest_clojure.h; rebase onto whichever lands first.

The problem, in user terms

In Clojure, control flow is a list whose head is a special form or a macro: (if …), (when …), (doseq [x xs] …). The grammar has no control nodes, only list_lit. ripwire's metrics walk looks for control node types, so it finds none: every Clojure function prints cx="1" and no nest. The --quality-delta kinds built on these (complexity, nesting, tangle, and the deep / humps facts) can never fire on Clojure.

Fixture probe.clj:

(ns probe.core)

(defn nested-doseq [xs ys]
  (doseq [x xs]
    (doseq [y ys]
      (when (= x y)
        (println x)))))

(defn plain [x]
  (inc x))

On main (e94757295), other attributes cut for reading:

ripwire . --no-cache --metrics
<s t="fn" n="nested-doseq" cx="1" ccx="0" nest="0"
<s t="fn" n="plain"        cx="1" ccx="0" nest="0"

Expected: nested-doseq reads like the same double loop plus if in every other language: nest="3" and cx="4" (1 + two loops + one when). plain stays cx="1".

Where to look

  • cc_walk in src/ingest_metrics.h:2191. Elixir has the same problem (control is an ordinary call) and already has a keyword channel: elixirKeyword is read from the call's target at line 2237, and elixirDecision / elixirControl (lines 2245-2246) feed the decision count (line 2247) and the nesting branch (line 2274). Clojure needs the same channel, with the list's head symbol as the keyword.
  • clojureListHead in src/ingest_clojure.h:18 reads the head symbol (it already skips comments, #_ discards and reader metadata). Note the include order: src/ingest.cpp:202 includes ingest_metrics.h before ingest_clojure.h (line 213), so the walk cannot call it as it stands. Move the helper earlier (a forward declaration in the same translation unit works too) and say which you chose.
  • isDecisionType (src/ingest_metrics.h:32) and cc_isNestingControl (line 107) are the node-type lists the other languages use; Clojure stays out of them, the same as Elixir.

Suggested approach

One small constexpr table of head symbols in the Clojure section, in two groups, mirroring Elixir:

group heads counts as
decision + nesting if, if-not, when, when-not, if-let, when-let, if-some, when-some, doseq, for, dotimes, while, loop +1 cx, one level deeper
nesting container cond, condp, case one level deeper; each arm a decision, like case in Ruby (check how you split arms and say so)

Do not count a list whose head is a local or a parameter, and keep quoted forms inert ('(if a b) is data; the existing quote and discard filter shows the rule). recur is not a loop by itself; loop is.

The test to add (red first)

  • Add the fixture to test/clojurecheck.sh / test/clojurefix/ (or a small test/clojuremetricscheck.sh, registered in test/regression.sh in the same commit or test/manifestcheck.sh fails). Assert nested-doseq prints nest="3" and cx="4", red on main.
  • Near misses that must not move: (println (if a b c)) counts the if but the println call adds nothing; '(when x y) and #_(when x y) count nothing; a function named when-ready called as (when-ready x) counts nothing (match the whole symbol, not a prefix); (defn plain …) stays cx="1".
  • The metrics are cached: bump kParserVer (src/ingest_cache.h:409) and its mirror kIngestParserVerMirror (src/quality.h:3335) to any number above main's, and run bash test/qschemetripcheck.sh.

Acceptance criteria

  • nested-doseq prints nest="3" cx="4"; every near miss holds.
  • bash test/clojurecheck.sh "$PWD/build/ripwire" and bash test/elixircheck.sh "$PWD/build/ripwire" (if you touched the shared channel) pass.
  • bash test/langidentitycheck.sh origin/main HEAD --langs=clj,ex,py,ts,go,cpp: every language except Clojure is SAME. Clojure DIFFs only on metric attributes, waived by --intended rows naming the new arm. If you have a real Clojure repository, add --repo=clj:<your clone> and report how many functions moved.
  • ripwire . --quality-delta=origin/main..HEAD gates 0. No new Lang::Clojure branch in shared code beyond the one channel next to Elixir's (CONTRIBUTING, "Definition of done for a common-code slice", item 6).

Size

S-M: a head table, one channel in the walk, a fixture, a version bump.

How to verify locally

cmake -S . -B build && cmake --build build -j      # plain build: no build type
mkdir -p /tmp/cljnest && cp probe.clj /tmp/cljnest/ && ./build/ripwire /tmp/cljnest --no-cache --metrics
bash test/clojurecheck.sh "$PWD/build/ripwire"
bash test/qschemetripcheck.sh
bash test/langidentitycheck.sh origin/main HEAD --langs=clj,ex,py,ts,go,cpp
./build/ripwire . --quality-delta=origin/main..HEAD --legend=compact

Not in scope

Threading macros (->, ->>), map / filter / reduce callbacks (callback loops are low in cx by design in every language), essential complexity ev for Clojure, and moving the head tables into the tags pass (CONTRIBUTING slice 5a).


Claiming it only takes a comment here. Every contributor is credited by handle in the CHANGELOG entry and the release notes. Thank you!

Lingua principale
C++
Stelle
2.6k
Fork
182
Merge medio
21h 12m
PR unite (30g)
262

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di redhat-et/ripwire

Tutte le issue di redhat-et/ripwire

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.