This is an archive of the discontinued LLVM Phabricator instance.

Make the diagnostic-missing-prototypes put the suggested `static` in front of `const` if exists.
ClosedPublic

Authored by oontvoo on Jun 8 2020, 6:36 PM.

Details

Summary

Consider: const int* get_foo() {return nullptr;}
The suggested fix should be static const int* get_foo(){}
and not const static int* get_foo(){}

Diff Detail

Event Timeline

oontvoo created this revision.Jun 8 2020, 6:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2020, 6:36 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gribozavr2 added inline comments.Jun 9 2020, 2:46 AM
clang/lib/Sema/SemaDecl.cpp
14252

Could you add tests for the following cases? I'm not sure the implementation will work.

// Two spaces between 'const' and 'struct'.
const  struct MyStruct get_struct() ...

const /*comment*/ struct MyStruct get_struct() ...

#define MY_CONST const
MY_CONST struct MyStruct get_struct() ...

I think a better way would be to check the token that comes before the type name, and if that token is 'const', then adjust the source location -- otherwise insert the 'static' keyword where we used to insert it in the past.

oontvoo updated this revision to Diff 269627.Jun 9 2020, 11:56 AM
oontvoo marked an inline comment as done.
  • Less brittle way to find the loc of const.
  • Add more tests
oontvoo marked an inline comment as done.Jun 9 2020, 11:57 AM
oontvoo added inline comments.
clang/lib/Sema/SemaDecl.cpp
14252

Done.

gribozavr2 added inline comments.Jun 9 2020, 12:56 PM
clang/lib/Lex/Lexer.cpp
1302 ↗(On Diff #269627)

I'm concerned about putting this API into Lexer, given this implementation. It is not a precise general-purpose implementation, it can have false negatives (for example, if the const token is formed through macros, or token pasting or whatnot), and it can have false positives (if the 'const' token is actually a macro that expands to something else).

I'd suggest to define this function as a local lambda right where it is used or inline it completely into findBeginLoc.

clang/test/Sema/warn-missing-prototypes.c
57

Why not return (void*)0;?

62

Did you mean to return a pointer to MyStruct? The top-level const is not meaningful.

63

Extra indentation? Please make it consistent with other tests.

71

cost => const

98

"Since we can't easily understand what MY_CONST means while preparing the diagnostic, the fix-it suggests to add 'static' in a non-idiomatic place."

oontvoo marked 7 inline comments as done.Jun 9 2020, 1:49 PM
oontvoo added inline comments.
clang/test/Sema/warn-missing-prototypes.c
62

No, I actually intended to test the top-level const. Though not being meaningful, it's still syntactically correct.
But now that you've mentioned it, I should add a test for MyStruct* too.

oontvoo updated this revision to Diff 269657.Jun 9 2020, 1:49 PM
oontvoo marked an inline comment as done.

Address review comment

gribozavr2 accepted this revision.Jun 9 2020, 2:48 PM
This revision is now accepted and ready to land.Jun 9 2020, 2:48 PM
This revision was automatically updated to reflect the committed changes.