Conference Proceeding Article
The widely used C preprocessor (CPP) is generally considered a source of difficulty for understanding and maintaining C/C++ programs. The main reason for this difficulty is CPP’s purely lexical semantics, i.e., its treatment of both input and output as token streams. This can easily lead to errors that are difficult to diagnose, and it has been estimated that up to 20% of all macros are erroneous. To reduce such errors, more restrictive, replacement languages for CPP have been proposed to limit expanded macros to be valid C syntactic units. However, there is no practical tool that can effectively validate CPP macros in legacy applications. In this paper, we introduce a novel, general characterization of inconsistent macro usage as a strong indicator of macro errors. Our key insight is that all applications of the same macro should behave similarly. In particular, we map each macro call c in a source file f to c’s normalized syntactic constructs within the abstract syntax tree (AST) for f’s preprocessed source, and use syntactic similarity as the basis for comparing macro calls of the same macro definition. Utilizing this characterization, we have developed an efficient algorithm to statically validate macro usage in C/C++ programs. We have implemented the algorithm; evaluation results show that our tool is effective in detecting common macro-related errors and reports few false positives, making it a practical tool for validating macro usage.
preprossing, macro errors, inconsistencies
Software and Cyber-Physical Systems
2009 IEEE/ACM International Conference on Automated Software Engineering: ASE 2009: Proceedings: Auckland, New Zealand, 16-20 November 2009
IEEE Computer Society
City or Country
Los Alamitos, CA
SAEBJORNSEN, Andreas; JIANG, Lingxiao; Quinlan, Daniel; and SU, Zhendong.
Static validation of C preprocessor macros. (2009). 2009 IEEE/ACM International Conference on Automated Software Engineering: ASE 2009: Proceedings: Auckland, New Zealand, 16-20 November 2009. 149-160. Research Collection School Of Information Systems.
Available at: http://ink.library.smu.edu.sg/sis_research/495
Copyright Owner and License
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 4.0 License.