This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Implement IWYU begin_keep/end_keep pragma support.
ClosedPublic

Authored by VitaNuo on Nov 28 2022, 5:36 AM.

Details

Summary

Implement support for begin_keep/end_keep pragmas.

Diff Detail

Event Timeline

VitaNuo created this revision.Nov 28 2022, 5:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 5:36 AM
Herald added a subscriber: kadircet. · View Herald Transcript
VitaNuo requested review of this revision.Nov 28 2022, 5:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 5:36 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
VitaNuo updated this revision to Diff 478203.Nov 28 2022, 5:40 AM

Add a comment.

hokein added inline comments.Nov 28 2022, 6:47 AM
clang-tools-extra/include-cleaner/lib/Record.cpp
220

nit: remove the brace for the if, the same below.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
354

with more tests being added, these line-number-inlined tests now are hard to follow, even with the line comment in the example.

I think we can polish these tests by using the annotation point ^, something like.

llvm::Annotation Code = R"cpp(
$keep1^#include "keep1.h" // IWYU pragma: keep

$normal^%include "normal.h"
)cpp";

EXPECT_TRUE(PI.shouldKeep(offsetToLine(Code.point("keep1"))));
EXPECT_FALSE(PI.shouldKeep(offsetToLine(Code.point("normal"))));

We need to implement an offsetToLine function, it should be trivial to add (basically calculate the number of \ns to the offset point).

VitaNuo updated this revision to Diff 478256.Nov 28 2022, 8:37 AM

Address review comments. Use annotation points instead of line numbers.

VitaNuo marked an inline comment as done.Nov 28 2022, 8:38 AM
VitaNuo added inline comments.
clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
354

Ok, thanks. Have a look now please.

VitaNuo updated this revision to Diff 478483.Nov 29 2022, 12:54 AM

Fix formatting.

hokein added inline comments.Nov 29 2022, 4:06 AM
clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
312

this is only used in IWYUKeep test, I'd make it as a local lambda (with MainFile captured) in the test:

auto OffsetToLineNum = [&MainFile](size_t Offset) { ... };
349

I think we can also do similar thing on $begin_keep_pragma^// IWYU pragma...

351

keep4.h can be removed to simplify the testcode.

358

keep7.h can be removed.

365

no need to repeatly test the begin_keep and end_keep pragmas.

VitaNuo updated this revision to Diff 478545.Nov 29 2022, 5:47 AM
VitaNuo marked 6 inline comments as done.

Address review comments.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
312

Nice idea, thanks.

hokein accepted this revision.Nov 29 2022, 6:15 AM

thanks, looks good.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
312

this method can be removed.

This revision is now accepted and ready to land.Nov 29 2022, 6:15 AM
VitaNuo updated this revision to Diff 478561.Nov 29 2022, 6:25 AM

Remove excessive method.

VitaNuo marked an inline comment as done.Nov 29 2022, 6:26 AM
VitaNuo added inline comments.
clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
312

Oops, sorry.

This revision was landed with ongoing or failed builds.Nov 29 2022, 6:54 AM
This revision was automatically updated to reflect the committed changes.
VitaNuo marked an inline comment as done.