This is an archive of the discontinued LLVM Phabricator instance.

[Diagnostics] Warn for comparison with string literals expanded from macro (PR44064)
ClosedPublic

Authored by xbolva00 on Nov 22 2019, 3:23 PM.

Details

Summary

As noted in PR, we have a poor test coverage for this warning. I think macro support was just overlooked. GCC warns in these cases.
Clang missed a real bug in the code I am working with, GCC caught it.

Diff Detail

Event Timeline

xbolva00 created this revision.Nov 22 2019, 3:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 22 2019, 3:23 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
xbolva00 marked an inline comment as done.Nov 22 2019, 3:27 PM
xbolva00 added inline comments.
clang/test/Sema/warn-stringcompare.c
5–7

Current behaviour: https://godbolt.org/z/7nYH6N

aaron.ballman added inline comments.Nov 24 2019, 7:14 AM
clang/include/clang/Basic/DiagnosticSemaKinds.td
8432

I think this is a regression in the diagnostic because we dropped the recommendation for how to fix the issue. Why do we need to drop that? I'd be fine if it was made less specific, like: (use an explicit string comparison function instead) if the concern is over strncmp specifically.

clang/lib/Sema/SemaExpr.cpp
10368–10411

Why do we care about the macros here? It seems just as reasonable to warn on:

#define MACRO1 "one"
#define MACRO2 "one

if (MACRO1 == MACRO2) // Why not warn here too?
xbolva00 marked 2 inline comments as done.Nov 24 2019, 7:24 AM
xbolva00 added inline comments.
clang/include/clang/Basic/DiagnosticSemaKinds.td
8432

Yes, my concern was about strncmp. Your suggestion is fine.

clang/lib/Sema/SemaExpr.cpp
10368–10411

This “macro loc” check is for array comparison diagnostic. I preserved original logic of this function and enabled -Wstring-compare for macro locations.

We warn here (I will add a test).

aaron.ballman added inline comments.Nov 24 2019, 7:43 AM
clang/lib/Sema/SemaExpr.cpp
10368–10411

Ah, thank you!

xbolva00 updated this revision to Diff 230797.Nov 24 2019, 9:27 AM

Added new tests.
Improved recommendation how to fix warning.

This revision is now accepted and ready to land.Nov 24 2019, 10:25 AM

Thank you for the review

This revision was automatically updated to reflect the committed changes.