Skip to content

feat: clang-query based concept usage checker#568

Open
WhyPenguins wants to merge 2 commits intodoubtfire-lms:10.0.xfrom
WhyPenguins:clang-query-concept-usage-checker
Open

feat: clang-query based concept usage checker#568
WhyPenguins wants to merge 2 commits intodoubtfire-lms:10.0.xfrom
WhyPenguins:clang-query-concept-usage-checker

Conversation

@WhyPenguins
Copy link

Note - this PR isn't necessarily intended to be merged directly :) I'm unfamiliar with this project, and simply tried to guess at the relevant locations for these scripts. They will probably need to be relocated. They are also not in the language of the project - since they are relatively standalone I hope this is alright :)

Description

This PR adds a script check_concept_usage.py that uses clang-query to check if a C++ file matches any rules from a named rule set. It can be called as follows:

python check_concept_usage.py "submission.cpp" chapter_05_structuring_code desc

and it will output a list of one-line descriptions for each rule that was matched.

If it fails to run to completion, it will output errors on stderr, and leave stdout empty.

The rules are defined in rules.json - here's an example of the layout:

{
...
  "chapter_04_control_flow": {
    "includes": ["chapter_05_structuring_code"],
    "rules": [
      {
        "queries": ["functionDecl(isExpansionInMainFile(), unless(hasName(\"main\")), unless(cxxMethodDecl()))"],
        "id": "Function",
        "description": "Check that all your code is in the one function for now."
      },
      {
        "queries": ["cxxThrowExpr(isExpansionInMainFile())"],
        "id": "Exception",
        "description": "Simplify your error handling - you can achieve this in other ways."
      }
    ]
  },

  "chapter_03_data": {
   ...
}

Each rule set is named (e.g "chapter_04_control_flow"), and contains a list of rules. Each rule contain a list of queries to try for that rule, and an ID and description/message that can be output when that rule is matched. Each rule set can also include the rules from another rule set.

Type of change

  • New feature (non-breaking change which adds functionality)
  • [?] This change requires a documentation update

How Has This Been Tested?

A test script has been included (concept_usage_test.py) that tests a set of representative (but shortened/slightly mangled) pieces of code from the Field Guide. It checks that they pass under the rule sets that are from their own chapter or later, and fail with rule sets from earlier chapters. It also checks the specific ids of the rule matches to ensure they are matching correctly.

That's not to say this is exhaustive - I found a bunch of edge cases while putting the tests together, so it is possible there are more 😅

Checklist:

  • My code follows the style guidelines of this project (it's in Python sorry, hopefully that's okay...)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation if appropriate
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have created or extended unit tests to address my new additions

@WhyPenguins WhyPenguins changed the title Clang query concept usage checker feat: clang-query concept usage checker Feb 10, 2026
@WhyPenguins WhyPenguins changed the title feat: clang-query concept usage checker feat: clang-query based concept usage checker Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant