[all-commits] [llvm/llvm-project] fcde3d: [clangd] Patch PP directives to use stale preamble...

kadir çetinkaya via All-commits all-commits at lists.llvm.org
Fri May 29 03:48:06 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: fcde3d5b04b612ebc4164fe8f3e83f93cd1fce53
      https://github.com/llvm/llvm-project/commit/fcde3d5b04b612ebc4164fe8f3e83f93cd1fce53
  Author: Kadir Cetinkaya <kadircet at google.com>
  Date:   2020-05-29 (Fri, 29 May 2020)

  Changed paths:
    M clang-tools-extra/clangd/Preamble.cpp
    M clang-tools-extra/clangd/Preamble.h
    M clang-tools-extra/clangd/unittests/PreambleTests.cpp

  Log Message:
  -----------
  [clangd] Patch PP directives to use stale preambles while building ASTs

Summary:
Depends on D79930.

This enables more accurate parsing of the AST, by making new macro
definitions in preamble section visible. This is handled by injecting
define directives into preamble patch.

This patch doesn't handle any location mappings yet, so features like go-to-def,
go-to-refs and hover might not work as expected. These will be addressed in a
follow-up patch.

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D79992


  Commit: 538c2753f3ec818eae57a5c5dfbe1f05af57ee37
      https://github.com/llvm/llvm-project/commit/538c2753f3ec818eae57a5c5dfbe1f05af57ee37
  Author: Kadir Cetinkaya <kadircet at google.com>
  Date:   2020-05-29 (Fri, 29 May 2020)

  Changed paths:
    M clang-tools-extra/clangd/Headers.cpp
    M clang-tools-extra/clangd/Hover.cpp
    M clang-tools-extra/clangd/Preamble.cpp
    M clang-tools-extra/clangd/Preamble.h
    M clang-tools-extra/clangd/SourceCode.cpp
    M clang-tools-extra/clangd/SourceCode.h
    M clang-tools-extra/clangd/XRefs.cpp
    M clang-tools-extra/clangd/unittests/HeadersTests.cpp
    M clang-tools-extra/clangd/unittests/PreambleTests.cpp

  Log Message:
  -----------
  [clangd] locateMacroAt handles patched macros

Summary: Depends on D79992.

This patch changes locateMacroAt to perform #line directive substitution
for macro identifier locations.

We first check whether a location is inside a file included through
built-in header. If so we check whether line directive maps it back to
the main file, and afterwards use TokenBuffers to find exact location of
the identifier on the line.

Instead of performing the mapping in locateMacroAt, we could also store
a mapping inside the ParsedAST whenever we use a patched preamble. But
that would imply adding more responsibility to ParsedAST and paying for
the mapping even when it is not going to be used.

====

Go-To-Definition:

Later on these locations are used for serving go-to-definition requests,
this enables jumping to definition inside the preamble section in
presence of patched macros.

=====

Go-To-Refs:

Macro references in main file are collected separetely and stored as a
map from macro's symbol id to reference ranges. Those ranges are
computed inside PPCallbacks, hence we don't have access to TokenBuffer.

In presence of preamble patch, any reference to a macro inside the
preamble section will unfortunately have the wrong range. They'll point
into the patch rather than the main file. Hence during findReferences,
we won't get any ranges reported for those.

Fixing those requires:
- Lexing the preamble section to figure out "real range" of a patched
  macro definition
- Postponing range/location calculations until a later step in which we
  have access to tokenbuffers.

This patch trades some accuracy in favor of code complexity. We don't do
any patching for references inside the preamble patch but get any
reference inside the main file for free.

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80198


Compare: https://github.com/llvm/llvm-project/compare/9ab7215846f6...538c2753f3ec


More information about the All-commits mailing list