Source-to-source Transformation with Clang: Traversing the AST to Uniquely Identify and Mark C++ Macros

ARA is a whole-system compiler to optimize the system's nonfunctional properties with global knowledge about the interaction between application and operating system. It consists of two parts: First, it statically analyzes a (real-time) system's source code to retrieve global knowledge based on system calls. Equipped with this knowledge, it then synthesizes an optimized system image with improved nonfunctional properties like reduced memory usage.

To simplify source-code information extraction, it does not analyze the source code directly but a slightly preprocessed form, namely LLVM intermediate code. This allows to not implement C- and C++-parsers, but has the side effect to loose all information of preprocessor macros.

Imagine this example:

#define enable_irq() \
    do { \
        asm volatile ("sti\n\t nop\n\t" ::: "memory"); \
    } while (0)

int main() {
    enable_irq()
}

enable_irq() acts like a system call but never reaches a deeper compiler stage than the preprocessor. Therefore, the information is lost for ARA.

Goal of this thesis is to change that by implementing an automatic marking system within the preprocessor. The basic idea is to automatically embed a specific source code snippet in every function-like preprocessor macro. This snippet should have three properties:

  1. Don't crash other compilation stages.
  2. Should be thrown away by the compiler's optimizer (in a later stage).
  3. Should identify the macro.

Your tasks in this thesis are to construct a snippet with these properties, extend the C preprocessor within Clang to embed the snippet and test it's functionality with appropriate real world programs.

Topics covered by this thesis:
C preprocessor, whole system compiler, static analysis, LLVM, Clang, C, C++

Further Reading

OSPERT Workshop B
ARA: Automatic Instance-Level Analysis in Real-Time Systems
Gerion Entrup, Benedikt Steinmeier, Christian DietrichProceedings of the 15th Annual Workshop on Operating Systems Platforms for Embedded Real-Time Applications (OSPERT '19)2019.
PDF [BibTex]
OSPERT Workshop B Best Paper Award
Levels of Specialization in Real-Time Operating Systems
Björn Fiedler, Gerion Entrup, Christian Dietrich, Daniel LohmannProceedings of the 14th Annual Workshop on Operating Systems Platforms for Embedded Real-Time Applications (OSPERT '18)2018Best Paper Award.
PDF [BibTex]