TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Auditable Macros in C Code

55 pointsby praxis23over 7 years ago

4 comments

bluetomcatover 7 years ago
TL;DR<p>Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don&#x27;t want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like:<p><pre><code> #include &quot;foo_defs.h&quot; #define BAR 42 FOO; BAR; </code></pre> We filter out the include line and then preprocess the file, getting the following output:<p><pre><code> FOO; 42;</code></pre>
评论 #15773193 未加载
评论 #15772605 未加载
makecheckover 7 years ago
My uses of macros in C++ these days boil down to a few common use cases that are still insanely not handled well by the language:<p>0. Anything involving file and line number.<p>1. Generating a string version of something alongside the value of something, without repeating myself.<p>2. Creating printing macros to keep me from typing out the entire asinine syntax for something like “std::cerr &lt;&lt; foo &lt;&lt; bar &lt;&lt; baz &lt;&lt; std::endl” so I only need PRINT(foo &lt;&lt; bar &lt;&lt; baz). Substituting code fragments is dead simple with a macro and absurdly complex or impossible with other C++ mechanisms. Ironically I’m only doing it because of the poor design of the entire “iostream” stack.
glandiumover 7 years ago
Relatedly, with all the clang-based tooling like clang-tidy or clang-format, it&#x27;s kind of disappointing that there isn&#x27;t a tool to expand arbitrary macros in a source.<p>That would have been extremely useful when I was recently refactoring some old C code. I ended up writing a small script that can expand one macro at a time, but I wish I hadn&#x27;t had to.<p>I&#x27;m sure that script doesn&#x27;t work in the general case.
评论 #15773755 未加载
iOSGuyover 7 years ago
I try to use ObjC PPC macros for things that are constant for the run of the program, and can be reasoned about globally.<p>For example, some good ones I use are isLandscape or isIPad, which are both variable depending on the user, but constant for the run of the app. Perfect for a PCH file I think.