{"id":13419442,"url":"https://github.com/ltcmelo/psychec","last_synced_at":"2025-05-15T17:05:23.483Z","repository":{"id":12915818,"uuid":"73128877","full_name":"ltcmelo/psychec","owner":"ltcmelo","description":"A compiler frontend for the C programming language","archived":false,"fork":false,"pushed_at":"2025-05-04T14:46:46.000Z","size":11653,"stargazers_count":544,"open_issues_count":10,"forks_count":40,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-04T15:35:09.821Z","etag":null,"topics":["c","c-language","clanguage","compiler","compiler-frontend","language-semantics","parser","parsing","parsing-library","program-analysis","static-analysis","type-inference"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ltcmelo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-11-07T23:02:46.000Z","updated_at":"2025-05-04T14:46:47.000Z","dependencies_parsed_at":"2024-01-03T00:07:13.000Z","dependency_job_id":"b0bb9764-3e52-4658-a10c-9a964be54d93","html_url":"https://github.com/ltcmelo/psychec","commit_stats":{"total_commits":576,"total_committers":8,"mean_commits":72.0,"dds":"0.34895833333333337","last_synced_commit":"cbb2fa329ec93eaa33afaca900f3407201e5732a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltcmelo%2Fpsychec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltcmelo%2Fpsychec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltcmelo%2Fpsychec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltcmelo%2Fpsychec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ltcmelo","download_url":"https://codeload.github.com/ltcmelo/psychec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384988,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-language","clanguage","compiler","compiler-frontend","language-semantics","parser","parsing","parsing-library","program-analysis","static-analysis","type-inference"],"created_at":"2024-07-30T22:01:16.082Z","updated_at":"2025-05-15T17:05:18.469Z","avatar_url":"https://github.com/ltcmelo.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings","C++"],"sub_categories":[],"readme":"![](https://github.com/ltcmelo/psychec/workflows/build/badge.svg)\n![](https://github.com/ltcmelo/psychec/workflows/test-suite/badge.svg)\n\nC language (draft) proposal [Enabling Generic Functions and Parametric Types in C](https://ltcmelo.com/n2698.pdf); prototype available [here](http://www.genericsinc.info/index.php).\n\n![](https://docs.google.com/drawings/d/e/2PACX-1vT-pCvcuO4U63ERkXWfBzOfVKwMQ_kh-ntzANYyNrnkt8FUV2wRHd5fN6snq33u5hWmnNQR3E3glsnH/pub?w=375\u0026h=150)\n\n\n# Psyche-C\n\nPsyche-C is a compiler frontend for the C language.\nIt is designed as a library, with a focus on the implementation of static analysis tools of C programs, without requiring understanding of their build setup.\nBellow are the main characteristics of Psyche-C:\n\n- Clean separation between the syntactic and semantic compiler phases.\n- Algorithmic and heuristic syntax disambiguation.\n- Optional type inference for missing `struct`, `union`, `enum`, and `typedef` (due to `#include` failures).\n- API inspired by that of the [Roslyn .NET compiler](https://github.com/dotnet/roslyn) and [LLVM's Clang frontend](https://clang.llvm.org/).\n\n## Library and API\n\nPsyche-C's native API is in C++, but APIs for other languages are [planned](https://github.com/ltcmelo/psychec/issues/112).\n\n```cpp\nvoid analyse(const SourceText\u0026 srcText, const FileInfo\u0026 fi)\n{\n    ParseOptions parseOpts;\n    parseOpts.setAmbiguityMode(AmbiguityMode::DisambiguateAlgorithmically);\n\n    auto tree = SyntaxTree::parseText(srcText,\n                                      TextPreprocessingState::Preprocessed,\n                                      TextCompleteness::Fragment,\n                                      parseOpts,\n                                      fi.fileName());\n\n    auto compilation = Compilation::create(\"code-analysis\");\n    compilation-\u003eaddSyntaxTree(tree.get());\n\n    AnalysisVisitor analysis(tree.get(), compilation-\u003esemanticModel(tree.get()));\n    analysis.run(tree-\u003etranslationUnitRoot());\n}\n```\n\n```cpp\nSyntaxVisitor::Action AnalysisVisitor::visitFunctionDefinition(const FunctionDefinitionSyntax* node) override\n{\n    const sym = semaModel-\u003edeclaredSymbol(node);\n    if (sym-\u003ekind() == SymbolKind::Function) {\n        const FunctionSymbol* funSym = sym-\u003easFunction();\n        // ...\n    }\n    return Action::Skip;\n}\n\n```\n\n\n## The *cnippet* Driver\n\nPsyche-C comes with the *cnippet* driver and it can be used as an ordinary parser for C snippets.\nFor instance, if you compile the snippet below with *cnippet*, you'll see a diagnostic similar to the one you'd see with GCC or Clang.\n\n```c\nvoid f()\n{\n    int ;\n}\n```\n\n```\n~ cnip test.c\ntest.c:4:4 error: declaration does not declare anything\nint ;\n    ^\n```\n\n\n## Type Inference\n\nPsyche-C can optionally (so far only available in the [original branch](https://github.com/ltcmelo/psychec/tree/original)) infer the missing types of a C snippet.\nFor instance, for the snippet below, Psyche-C can infer a (compilable) declaration for `T` an synthesize it during compilation.\n\n```c\nvoid f()\n{\n    T v = 0;\n    v-\u003evalue = 42;\n    v-\u003enext = v;\n}\n```\n\n\n```c\ntypedef struct TYPE_2__ TYPE_1__;\nstruct TYPE_2__ \n{\n    int value;\n    struct TYPE_2__* next;\n} ;\ntypedef TYPE_1__* T;\n```\n\nYou might want to use this functionality to:\n\n- Enable, on incomplete programs, static analysis tools that require complete programs.\n- Compile a snippet (e.g. retrieved from a bug tracker) for object-code inspection.\n- Generate test-input data for a function in isolation..\n- Prototype an algorithm without specific data-structures.\n\n\n## Documentation and Resources\n\n- The Doxygen-generated [API](https://ltcmelo.github.io/psychec/api-docs/html/index.html).\n- A contributor's [wiki](https://github.com/ltcmelo/psychec/wiki).\n- An [online interface](http://cuda.dcc.ufmg.br/psyche-c/) that offers a glimpse of Psyche-C's type inference functionality.\n- Articles/blogs:\n  - [Dumping a C program’s AST with Psyche-C](https://ltcmelo.github.io/psychec/2021/03/03/c-ast-dump-psyche.html)  \n    (pt-BR) [Visualizando a AST de um programa C com o Psyche-C](https://www.embarcados.com.br/visualizando-a-ast-psyche-c/)\n  - [Programming in C with type inference](https://www.codeproject.com/Articles/1238603/Programming-in-C-with-Type-Inference)  \n    (pt-BR) [Programando em C com inferência de tipos usando PsycheC](https://www.embarcados.com.br/inferencia-de-tipos-em-c-usando-psychec/)\n\n\n## Building and Testing\n\nExcept for type inference, which is written in Haskell, Psyche-C is written in C++17; *cnippet* is written in Python 3.\n\nTo build:\n\n    cmake CMakeLists.txt \u0026\u0026 make -j 4\n\nTo run the tests:\n\n    ./test-suite\n\n\n## Related Publications\n\nOf Psyche-C itself:\n\n- [Type Inference for C: Applications to the Static Analysis of Incomplete Programs](https://dl.acm.org/doi/10.1145/3421472)\u003cbr/\u003e\nACM Transactions on Programming Languages and Systems — **TOPLAS**, Volume 42, Issue 3, Article No. 15, Dec. 2020.\n\n- [Inference of static semantics for incomplete C programs](https://dl.acm.org/doi/10.1145/3158117)\u003cbr/\u003e\nProceedings of the ACM on Programming Languages, Volume 2, Issue **POPL**, Jan. 2018, Article No. 29.\n\nThat use Psyche-C:\n\n- [SLaDe: A Portable Small Language Model Decompiler for Optimized Assembly](https://ieeexplore.ieee.org/abstract/document/10444788)\u003cbr/\u003e\nProceedings of the IEEE/ACM International Symposium on Code Generation and Optimization - **CGO**, 2024.\n\n- [AnghaBench: a Suite with One Million Compilable C Benchmarks for Code-Size Reduction](https://conf.researchr.org/info/cgo-2021/accepted-papers)\u003cbr/\u003e\nProceedings of the IEEE/ACM International Symposium on Code Generation and Optimization — **CGO**, 2021.\n\n- [Generation of in-bounds inputs for arrays in memory-unsafe languages](https://dl.acm.org/citation.cfm?id=3314890)\u003cbr/\u003e\nProceedings of the IEEE/ACM International Symposium on Code Generation and Optimization — **CGO**, Feb. 2019, p. 136-148.\n\n- [Automatic annotation of tasks in structured code](https://dl.acm.org/citation.cfm?id=3243200)\u003cbr/\u003e\nProceedings of the International Conference on Parallel Architectures and Compilation Techniques — **PACT**, Nov. 2018, Article No. 31.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltcmelo%2Fpsychec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fltcmelo%2Fpsychec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltcmelo%2Fpsychec/lists"}