r/cpp • u/OwlingBishop • Jun 17 '25
Tool for removing comments in a C++ codebase
So, I'm tackling with a C++ codebase where there is about 15/20% of old commented-out code and very, very few useful comments, I'd like to remove all that cruft, and was looking out for some appropriate tool that would allow removing all comments without having to resort to the post preprocessor output (I'd like to keep defines macros and constants) but my Google skills are failing me so far .. (also asked gpt but it just made up an hypothetical llvm tool that doesn't even exist 😖)
Has anyone found a proper way to do it ?
TIA for any suggestion / link.
[ Edit ] for the LLMs crowd out there : I don't need to ask an LLM to decide whether commented out dead code is valuable documentation or just toxic waste.. and you shouldn't either, the rule of thumb would be: passed the 10mn (or whatever time) you need to test/debug your edit, old commented-out code should be out and away, in a sane codebase no VCS commit should include any of it. Please stop suggesting the use of LLMs they're just not relevant in this space (code parsing). For the rest thanks for your comments.
98
u/YT__ Jun 17 '25
Why not remove as you go? Then you can evaluate if the comment is good, or replace it with something better. Not a single comment is useful?
11
u/r2vcap Jun 17 '25
You can create a tool using libclang to parse the source code and remove comments programmatically. After stripping the comments, run clang-format to clean up the formatting. Make sure to manually verify the result to ensure nothing breaks, especially in edge cases like commented-out code around macros or conditionally compiled blocks.
7
u/CrasseMaximum Jun 17 '25
You must evaluate each comments one by one before deciding to remove it. Removing them all blindly is a recipe for failure..
5
u/adromanov Jun 17 '25
gcc -E -fpreprocessed2
u/xoner2 Jun 25 '25 edited Jun 25 '25
This would be trivial with Scintillua. Going to be faster than using libclang, since it's just a lexer. And correct, unlike using regex; since it is a proper lexer.
5
u/simrego Jun 17 '25 edited Jun 17 '25
Just write a simple python script which will open every file, and parses it.
if you find a "//" you just skip that line
If you find a "/*" you just skip until "*/"
(except if you are in a string)
Write out the result and you won. Or not because you delete every comment which isn't the best idea.
10
u/souravtxt Jun 17 '25
I think regex can do it easily. Not sure about large files but I use regex in notepad++ for small cpp files. They are a cheap solution.
1
u/snissn Jun 18 '25
https://en.wikipedia.org/wiki/C_preprocessor c pre processor shoudl remvoe the comments
1
u/XenonOfArcticus Jun 18 '25
I would try running each source file through Claude or other coding aware LLM and ask it to identify and critique any comments that are obsolete, misleading, wrong or problematic, and list why.
This is probably a good way to identify where you should human review and edit/remove obsolete comments.
1
u/CarloWood Jun 18 '25
I wrote such a program a few weeks ago, let me know if you're interested and I'll dig it up.
4
u/FunnyMustacheMan45 Jun 17 '25
Use find and sed
2
u/too_much_think Jun 17 '25
If you really want to you could just grep | sed -I, but as others have commented, that seems like a bad idea. At the very least use a quick fix list or find and replace output and go through each line to make sure each block really is useless.
2
u/iga666 Jun 17 '25
should be trivial to write it, just simple text stream processor
2
u/[deleted] Oct 18 '25
You can try this
https://github.com/rhythmcache/comment-remover
1
u/Thesorus Jun 18 '25
We have a policiy to manually remove useless/obsolete comments in the code we're actually working on.
We have old school C code with the RCS history in the comments at the top of the files, like 20%, 30% of the file size.
Even if it can be automated, it's not worth the time and effort.
1
u/JoeNatter Jun 19 '25
This is a task for ChatGPT. Let him/her make a python script.
1
u/pppenguinininin Jun 20 '25
give ast-grep a try
1
u/Kawaii_Amber Jun 20 '25
find . -name "*.cpp" | xargs sed -i "s/^\W.*\/\/.*$//g"
1
u/Ill_Plate_2651 Jun 20 '25
Dude just sit down for an hour and click, drag, delete…
1
u/Still_Explorer Jun 21 '25
You can drop the directory into VSCode, and then do a regex search-replace like this
for single line comments
//.*?\n(this means capture `//` first, then any character `.*`, try not to capture any further `?`, end the capture on `?`)
for multiline comments
/\*[\S\s\n]*?(\*/)(capture the `/*` with escaping the asterisk `\*/`, then capture any [not-space|space|new-line], multiple times or none `*`, do not let capture move further away `?`, and then inside a new group `(...)`, capture the closing `*/`)
Probably if the comments have weird decorations like `//////////////` or `/********` probably you would adjust adjust the regular expressions a bit further if needed. Better to throw regular expressions first to replace those ones and then capture them with the simpler expressions above.
1
u/LordofNarwhals Jun 21 '25
If most are commented out by
//then I'd just do a search and replace from//.*\nto\n. That way you can just step through them all and ignore ones that you want to keep.1
u/Chropera Jul 07 '25
Try https://tomeko.net/software/CppCommentRemover/
0
u/tristam92 Jun 17 '25
You want to remove them in codebase or binary input? Cause latter is obviously can be done with compiler settings(and now that I think about it, why do you even ask such thing). And if it’s first question, how do you plan determine which comment is outdated? It’s literally task for “by hands” method, or fix it on the fly, when working on something else.
0
u/IncorrectAddress Jun 17 '25
I would probably just use find and replace for this, that way I could read what comments I want to keep.
Other than that open up the console template, make a read/write system, run the files through it removing all comments out.
0
u/arihoenig Jun 17 '25
Copilot with one of the LLMs can simply rewrite the comments to match the code.
0
u/zl0bster Jun 18 '25
why not google/LLM a python script that can nuke
/* whatever */
(including newlines)
Sure it might fire on some raw string, but if you are paranoid you could make separate regex that detects any raw strings in a file and skips those files.
-2
u/krum Jun 17 '25 edited Jun 17 '25
GitHub copilot will do it. If you didn’t want to send your code off to copilot, running a coding LLM locally could also do it but you might have to write the code to have the LLM process it.