This document discusses code style best practices for compilers, including using language features like enums and typedefs to improve safety and avoid bugs, following guidelines like MISRA C and CERT, and using static and dynamic analysis tools. It also discusses strategies for handling legacy code and notes safer languages like Spark Ada which use formal methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13K views
Code Style For Compilers: 18-642 / Fall 2020
This document discusses code style best practices for compilers, including using language features like enums and typedefs to improve safety and avoid bugs, following guidelines like MISRA C and CERT, and using static and dynamic analysis tools. It also discusses strategies for handling legacy code and notes safer languages like Spark Ada which use formal methods.
Let the Language Help! Use enum instead of int enum color {black, white, red}; // avoids bad values Use const instead of #define const uint64_t x = 1; // helps with type checking uint64_t y = x << 40; // avoids 32-bit overflow bug Use inline instead of #define If it’s too big to inline, the call overhead doesn’t matter Many compilers inline automatically even without keyword
Use typedef with static analysis
typedef uint32_t feet; typedef uint32_t meters; feet x = 15; meters y = x; // feet to meters assignment error https://goo.gl/6SqG2i
CODING FOR ABSOLUTE BEGINNERS: How to Keep Your Data Safe from Hackers by Mastering the Basic Functions of Python, Java, and C++ (2022 Guide for Newbies)