This is an archive of the discontinued LLVM Phabricator instance.

[Format][Tooling] Fix HeaderIncludes::insert not respect the main-file header.
ClosedPublic

Authored by hokein on Jul 11 2023, 6:35 AM.

Diff Detail

Event Timeline

hokein created this revision.Jul 11 2023, 6:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 6:35 AM
hokein requested review of this revision.Jul 11 2023, 6:35 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 11 2023, 6:35 AM
kadircet added inline comments.Jul 11 2023, 10:04 AM
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
339–341

instead of doing this unconditionally, can we do this until we find first include that looks like the main include? similarly we should also only look for main includes during ::insert if we didn't encounter one already during initial scan.

this ensures we give users a way out (in certain cases) when there are multiple alternatives, by respecting the order they have. it's also aligned with the behaviour of sortCppIncludes, which is used when formatting files elsewhere.

clang/unittests/Tooling/HeaderIncludesTest.cpp
146

can you also have test cases with competing includes, eg:

foo.cc, with contents:

#include "zoo/foo.h"
// insert "xoo/foo.h"

-> xoo/foo.h doesn't become main include

hokein updated this revision to Diff 539481.Jul 12 2023, 3:48 AM
hokein marked 2 inline comments as done.

address comments

kadircet added inline comments.Jul 12 2023, 4:19 AM
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
369–370

sorry if I was unclear in the previous comment. but i was trying to say we should only consider includes we've encountered during the initial scan.

getting different results depending on the order of insertions being performed is likely gonna cause confusion elsewhere (e.g. depending on the first diagnostic you've in clangd, you'll get different order of includes). hence, let's keep this function stateless (which also gets rid of the concerns around mutable)

hokein updated this revision to Diff 539506.Jul 12 2023, 4:54 AM

Make the MainFileFound stateless.

hokein marked an inline comment as done.Jul 12 2023, 4:56 AM
hokein added inline comments.
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
369–370

sorry for misunderstanding your comment. Make it stateless looks like a good idea.

kadircet accepted this revision.Jul 12 2023, 9:16 AM
This revision is now accepted and ready to land.Jul 12 2023, 9:16 AM
This revision was landed with ongoing or failed builds.Jul 13 2023, 1:50 AM
This revision was automatically updated to reflect the committed changes.
hokein marked an inline comment as done.