Discussion
Loading...

Discussion

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Michael Dexter
@dexter@bsd.network  ·  activity timestamp 2 weeks ago

Do you have a favorite method of removing C-style inline and multiline comments from code?

Removing trailing tabs that were before them and resulting double lines is a plus.

<3

  • Copy link
  • Flag this post
  • Block
Tim Chase
@gumnos@mastodon.bsd.cafe replied  ·  activity timestamp 2 weeks ago
@dexter

IIUC,

$ gcc -fpreprocessed -dD -E -P file.c > no_comments.c

should do the trick. You really need a proper C/C++ aware lexer/parser because while simple substitutions

:%s@\_s*//.*\n/\r

in Vim can get thrown off by "//" or "/*…*/" embedded in string-constants. There are additional complexities with multi-line strings and nested comments. I'm not sure if you also intend to take into consideration ": #if 0" blocks.

  • Copy link
  • Flag this comment
  • Block
Garrett Wollman
@wollman@mastodon.social replied  ·  activity timestamp 2 weeks ago
@dexter Hmmm, I think I'd do it in sed, but probably not 100% accurately.
  • Copy link
  • Flag this comment
  • Block
Garrett Wollman
@wollman@mastodon.social replied  ·  activity timestamp 2 weeks ago
@dexter Block comments are particularly hard for anything that processes line-at-a-time and tries to use regular expressions, even though it's a trivial DFA when processing character-by-character. Perl's multiline REs could do it easily if you can read the entire input into a scalar.
  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0-rc.2.21 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login