This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Improve google-objc-function-naming diagnostics πŸ“™
ClosedPublic

Authored by stephanemoore on Dec 8 2018, 7:42 PM.

Details

Summary

The diagnostics from google-objc-function-naming check will be more
actionable if they provide a brief description of the requirements from
the Google Objective-C style guide. The more descriptive diagnostics may
help clarify that functions in the global namespace must have an
appropriate prefix followed by Pascal case (engineers working previously
with static functions might not immediately understand the different
requirements of static and non-static functions).

Test Notes:
Verified against the clang-tidy tests.

Diff Detail

Event Timeline

stephanemoore created this revision.Dec 8 2018, 7:42 PM
MyDeveloperDay added inline comments.
test/clang-tidy/google-objc-function-naming.m
8

I realize there are words that begin with 'is...' but you could detect if the function returned a boolean and started with "is,has,does" and could this extrapolate to IsPositive() HasSomething(), DoesHaveSomething(), this might generate a better fixit candidate? (just a suggestion feel free to ignore)

aaron.ballman added inline comments.Dec 10 2018, 8:14 AM
clang-tidy/google/FunctionNamingCheck.cpp
114–125

I'd rather see these diagnostics combined: %select{static|global}1 function name %0 must %select{be in |have an appropriate prefixed followed by "}1 Pascal case as required by the Google Objective-C style guide to simplify the logic (since generateFixItHint() already does the right thing).

stephanemoore marked 4 inline comments as done.

Use the select modifier to customize diagnostics for static and non-static function instead of branching diagnostic code paths for static and non-static functions.

benhamilton accepted this revision.Dec 10 2018, 3:45 PM

Thanks, just minor suggestions.

clang-tidy/google/FunctionNamingCheck.cpp
114–118

Should we suggest making the function static when it fails this check? (I assume the vast majority of functions which fail this check should really be static.)

115

I know "global" is the correct name, but I feel like "non-static" might be clearer to folks dealing with these error messages.

WDYT?

This revision is now accepted and ready to land.Dec 10 2018, 3:45 PM
stephanemoore added inline comments.Dec 10 2018, 3:49 PM
clang-tidy/google/FunctionNamingCheck.cpp
114–125

I had no idea about the select modifier! Good idea!

test/clang-tidy/google-objc-function-naming.m
8

Sounds like something worth investigating.

Filed https://bugs.llvm.org/show_bug.cgi?id=39941

stephanemoore marked 4 inline comments as done.Dec 10 2018, 4:15 PM
stephanemoore added inline comments.
clang-tidy/google/FunctionNamingCheck.cpp
114–118

Are you asking whether we should suggest making global functions static when they are missing appropriate prefixes? If so, I think that would lead to undesired changes in various cases.

One common cause of this style violation is when someone endeavors to take a static function that is used in one translation unit and share it with other translation units by migrating it to a header. The Easyβ„’ thing to do is migrate that static function to a header and convert it directly to a global function, avoiding the overhead of refactoring the function name and all call sites. The effort of the described refactoring approach is low compared to renaming the function to avoid symbol collisions in the global namespace as is generally recommended.

If a function's definition and declaration (if it exists) are both in an implementation file we can probably recommend making the function static though. Filed https://bugs.llvm.org/show_bug.cgi?id=39945.

115

I'm wary of saying "non-static" because namespaced functions in Objective-C++ are not subject to the cited rules. The term "non-static" seems like it could be interpreted to extend to namespaced functions which could potentially mislead someone into adding prefixes to namespaced functions in Objective-C++.

aaron.ballman added inline comments.Dec 11 2018, 10:34 AM
clang-tidy/google/FunctionNamingCheck.cpp
113

Drop the top-level const qualifier, please.

115

How about "%select{static function|function in global namespace}1 named %0..."

stephanemoore marked 5 inline comments as done.

Changes:
β€’ Drop const on local bool variable.
β€’ Adopt the term "function in global namespace" in diagnostic messages.

clang-tidy/google/FunctionNamingCheck.cpp
115

Definitely better.

benhamilton added inline comments.Dec 12 2018, 8:42 AM
clang-tidy/google/FunctionNamingCheck.cpp
115

Yeah, this is better. Thanks.

This revision was automatically updated to reflect the committed changes.