Skip to content

A compiler frontend for the C programming language

License

Notifications You must be signed in to change notification settings

the-slow-one/psychec

 
 

Repository files navigation

Check the open bounties and tasks!

Psyche-C

Psyche is a compiler frontend for the C programming language that is specifically designed for the implementation of static analysis tools. These are the distinct features that make Psyche-C rather unique:

  • Clean separation between the syntactic and semantic compiler phases.
  • Algorithmic- and heuristic-based syntax disambiguation.
  • Type inference for missing struct, union, enum, and typedef (i.e., tolerance against #include failures).
  • API inspired by that of the Roslyn .NET compiler.
  • Parser's AST resembling that of the LLVM's Clang frontend.

Library and API

Psyche-C offers a C++ library/API for the implementation of static analysis tools of C programs.

void analyse(const FileInfo& fi)
{
    auto tree = SyntaxTree::parseText(srcText,
                                      TextPreprocessingState::Preprocessed,
                                      ParseOptions(),
                                      fi.fileName());
    auto compilation = Compilation::create("code-analysis");
    compilation->addSyntaxTree(tree.get());

    CustomSyntaxVisitor analysis(tree.get(), compilation->semanticModel(tree.get()));
    analysis.run(tree->translationUnitRoot());
}

The cnippet Driver

Psyche-C comes with the cnippet driver and may be used as an ordinary C parser.

void f()
{
    int ;
}

If you parse the snippet above with cnippet, you'll see a diagnostic similar/equal to what you would see with GCC or Clang.

~ cnip test.c
test.c:4:4 error: declaration does not declare anything
int ;
    ^

Note: Semantic analysis isn't yet complete.

Type Inference

cnippet understands code snippets (a.k.a. as incomplete programs or program fragments) through Psyche-C's type inference.

void f()
{
    T v = 0;
    v->value = 42;
    v->next = v;
}

If you compile the snippet above with GCC or Clang, you'll see a diagnostic such as "declaration forTis not available".
With cnippet, "compilation" succeeds, as the following definitions are implicitly synthesised.

typedef struct TYPE_2__ TYPE_1__;
struct TYPE_2__ 
{
    int value;
    struct TYPE_2__* next;
} ;
typedef TYPE_1__* T;

These are a few application of type inference for C:

  • Enabling, on incomplete source-code, static analysis techniques that require fully-typed programs.
  • Compiling partial code (e.g., a snippet retrieved from a bug tracker) for object-code inspection.
  • Generating test-input data for a function in isolation (without its dependencies).
  • Quick prototyping of an algorithm, without the need of explicit types.

NOTE: Type inference isn't yet available on master, only in the original branch.

Documentation and Resources

Building and Testing

Except for type inference, which is written in Haskell, Psyche-C is written in C++17; cnippet is written in Python 3.

To build:

cmake CMakeLists.txt && make -j 4

To run the tests:

./test-suite

Related Publications

About

A compiler frontend for the C programming language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 94.2%
  • Haskell 2.3%
  • Python 1.4%
  • CMake 1.0%
  • C 0.8%
  • NASL 0.2%
  • Shell 0.1%