This is an archive of the discontinued LLVM Phabricator instance.

Add FreeBSD kernel printf extensions
ClosedPublic

Authored by dim on Jan 23 2015, 1:31 PM.

Details

Summary

In FreeBSD, we have been carrying around a custom patch for clang, since about four and a half years, to enable checking the FreeBSD kernel specific printf format specifiers: %b, %D, %r and %y. This patch was based on an earlier custom patch for gcc, with similar functionality, and it added a new -fformat-extensions option to enable those non-standard specifiers.

A few years ago we tried getting this patch upstreamed to llvm/clang, but it was not met with great enthusiasm. I think this was mostly because adding an option was seen as ugly, and even then, the option name did not convey that it was specifically meant for FreeBSD only.

I have now tried to massage the patch so that it is hopefully more palatable:

  • It adds a new freebsd_kprintf format string type, which enables checking when used in attribute((format(...))) attributes
  • Checks %b, %D, %r and %y specifiers, using existing diagnostic messages
  • Adds test cases for all these specifiers

Now, I know the printf format checking might be due for a big overhaul, and this patch is not the prettiest around, but please consider that it implements functionality that we really need, and use. So I would rather get this in, and have it be part of some later restructuring, instead of having to forward-port it each new release... :)

Diff Detail

Repository
rL LLVM

Event Timeline

dim updated this revision to Diff 18692.Jan 23 2015, 1:31 PM
dim retitled this revision from to Add FreeBSD kernel printf extensions.
dim updated this object.
dim edited the test plan for this revision. (Show Details)
dim added reviewers: rsmith, aaron.ballman, hans, joerg.
dim added subscribers: emaste, Unknown Object (MLST).
hans edited edge metadata.Jan 26 2015, 6:26 PM

FWIW, I don't think this is completely terrible..

I would prefer to pass in the FormatStringType instead of a bool though.

dim added a comment.Jan 26 2015, 11:57 PM
In D7154#113752, @hans wrote:

I would prefer to pass in the FormatStringType instead of a bool though.

Yes, I would have preferred that too, but since FormatStringType comes from include/clang/Sema/Sema.h, and the actual usage is in lib/Analysis/PrintfFormatString.cpp, I'd have to add includes to that latter file, and prefix everything with Sema:: there. It just made it all more messy, and I tried to avoid that.

I also thought of putting the FormatStringType into the CheckPrintfHandler class, to eliminate the need of passing this information via function parameters. But that leads to ugly upcasting (from FormatStringHandler up to CheckPrintfHandler) in lib/Analysis/PrintfFormatString.cpp again... I'm not sure what a good solution is for that. You also cannot put the FormatStringType into the FormatStringHandler class itself, since it does not really belong there.

dim added a comment.Feb 1 2015, 11:46 PM

Ping. Any other comments? :)

dim added a reviewer: bkramer.Feb 2 2015, 12:39 PM

Ben, can you please review this, and optionally commit it? I think this addition is really not a very heavy burden with regards to maintenance, since it has been virtually unchanged since about 8 or so releases. That is, unless the whole printf formatting parts get remodeled, and in that case I'll gladly help to fix up whatever needs to be fixed.

dim added a comment.Feb 16 2015, 12:13 PM

Ping 3...

This revision was automatically updated to reflect the committed changes.