This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Collect macros in the preamble region of the main file
ClosedPublic

Authored by hokein on Sep 12 2019, 6:27 AM.

Details

Summary
  • store all macro references in the ParsedAST, which also helps to implement find references for macros.
  • unify the two variants of CollectMainFileMacros.

Diff Detail

Repository
rL LLVM

Event Timeline

hokein created this revision.Sep 12 2019, 6:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 12 2019, 6:27 AM
hokein updated this revision to Diff 219904.Sep 12 2019, 6:33 AM

some cleanup

friendly ping, in case you missing it :)

Sorry for the delay

clang-tools-extra/clangd/Macro.h
59 ↗(On Diff #219904)

Converting here is too early, could we keep this conversion in syntax highlighting code?
Keeping source locations here is enough.

67 ↗(On Diff #219904)

Could we model in a way that avoids duplicating macro names on each occurrence?
We had StringSet Names and vector<SourceLocation> Locations, let's keep it in the same way.

We could group this into a struct to reduce boilerplate of transferring it around, obviously

struct MainFileMacros {
  StringSet Names;
  vector<SourceLocation> Locations;
};
hokein marked an inline comment as done.Sep 23 2019, 5:58 AM
hokein added inline comments.
clang-tools-extra/clangd/Macro.h
59 ↗(On Diff #219904)

I'm not sure this is doable -- to get a range, we need the SourceManager, in the syntax highlighting context, we get the SourceManager from the ParsedAST, this SourceManager is used when building the main AST with preamble, I don't think we can use this SourceManager to get the token range for macros in the preamble section?

67 ↗(On Diff #219904)

yes, we don't need the name and location reletionship in this patch, but we'd need this when implementing xrefs for macros. do you think we should keep the same way, and do the change when we start implementing xrefs for macros?

ilya-biryukov marked an inline comment as done.Sep 24 2019, 1:41 AM
ilya-biryukov added inline comments.
clang-tools-extra/clangd/Macro.h
59 ↗(On Diff #219904)

Ah, yeah, good point. The source manager from preamble is not there anymore.
Ranges LG and this is what we used before.

Thanks for clearing this up.

67 ↗(On Diff #219904)

Cross-references have to be more fine-grained and having only a name is not enough. Let's address this separately when/if we actually need this in xrefs.
In particular, we want to handle macro re-definitions:

#define FOO 123 // #1
int a = FOO; // a reference to #1

#define FOO 234 // #2
int b = FOO; // a reference to #2

Current model will merge these two together as both names are the same.

hokein updated this revision to Diff 221492.Sep 24 2019, 2:43 AM
hokein marked 2 inline comments as done.

Address comments.

hokein added inline comments.Sep 24 2019, 2:46 AM
clang-tools-extra/clangd/Macro.h
67 ↗(On Diff #219904)

good point, agreed. I think we probably use the symbol id for macros, but it is out-scope of this patch.

ilya-biryukov accepted this revision.EditedSep 24 2019, 2:55 AM

LGTM, thanks.

A ton of small NITs too, but they're all small details.

clang-tools-extra/clangd/Macro.h
1 ↗(On Diff #221492)

NIT: Maybe rename to CollectMacros.h?

Macro.h looks a bit too generic and may start being a place for more helpers. However, the current file is pretty well-structured.

23 ↗(On Diff #221492)

Could you add a comment that we have to convert to Range early because source manager from preamble is not available when we build the AST?

26 ↗(On Diff #221492)

NIT: a typo: in the main file. It is used ...

28 ↗(On Diff #221492)

NIT: use /// to enable doxygen comments

65 ↗(On Diff #221492)

NIT: It's probably safer to set 'no' by default.
Normally the first thing visited by the parser is <builtin> buffer, not the main file.

OTOH, FileChanged should be called first in any case, so it does not matter.

clang-tools-extra/clangd/ParsedAST.h
104 ↗(On Diff #221492)

NIT: rename parameter to Macros

124 ↗(On Diff #221492)

NIT: change definitions/expansions to definitions and expansion

clang-tools-extra/clangd/Preamble.cpp
34 ↗(On Diff #221492)

NIT: rename to takeMacros

73 ↗(On Diff #221492)

NIT: add const to SourceManager too, for consistency
(if possible)

clang-tools-extra/clangd/Preamble.h
61 ↗(On Diff #221492)

There seems to be no reason for moving constructor to the bottom of the class. Maybe keep as is?

This revision is now accepted and ready to land.Sep 24 2019, 2:55 AM
hokein updated this revision to Diff 221501.Sep 24 2019, 3:56 AM
hokein marked 10 inline comments as done.

Address review comments.

hokein updated this revision to Diff 221504.Sep 24 2019, 4:06 AM

update header guard.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptSep 24 2019, 4:12 AM