14

We use lint in our codebase at work for C/C++, I'm trying to start integrating clang-format in my workflow as well.

Unfortunately, lint occasionally requires annotations to ignore a specific check, either of the format:

/*lint -[annotation] */

or

//lint -[annotation]

Specifically, if there's a space between the opening token for the comment and 'lint', it doesn't recognize it as an annotation directive. Unfortunately, the default settings I have for clang-format see that as an error and helpfully insert the space.

Is there any way to get clang-format to recognize comments matching that pattern and leave them alone? Right now I'm using 3.4, but could upgrade if needed.

Joe Sunday
  • 178
  • 1
  • 6

2 Answers2

20

Clang-format has a `CommentPragmas' option that is

A regular expression that describes comments with special meaning, which should not be split into lines or otherwise changed.

When I put the following line in my .clang-format file, my Lint comments remain untouched.

CommentPragmas:  '^lint'

Other comments that still have "lint" in them, but are not Lint comments still get formatted.

John
  • 7,301
  • 2
  • 16
  • 23
17

You can disable clang-format for that section of your file by using:

int formatted_code;
// clang-format off
    void    unformatted_code  ;
// clang-format on
void formatted_code_again;

See the Disabling formating on a piece of code section.

Yoh Deadfall
  • 2,711
  • 7
  • 28
  • 32
MikecheckDev
  • 2,176
  • 2
  • 15
  • 6
  • 1
    Any idea when this feature became available? I've been unable to successfully build on mac so using a prebuilt version _clang-format version 3.5 (tags/checker/checker-276)_ and these do not seem to have any affect. – user1338952 Dec 18 '14 at 17:41
  • Wed Aug 6 08:40:26 2014: [r214966 - clang-format: Add special comments to disable formatting.](http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20140804/111937.html), 03 Sep 2014: [Release of Clang 3.5.0](http://llvm.org/releases/) – rettichschnidi Jan 12 '16 at 22:11