[PATCH] D28772: [Preprocessor] Fix incorrect token caching that occurs when lexing _Pragma in macro argument pre-expansion mode when skipping a function body

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 16 07:29:07 PST 2017


arphaman created this revision.
arphaman added reviewers: bruno, rsmith, akyrtzi.
arphaman added a subscriber: cfe-commits.
arphaman set the repository for this revision to rL LLVM.
Herald added a subscriber: nemanjai.

This patch fixes a token caching problem that currently occurs when clang is skipping a function body (e.g. when looking for a code completion token) and at the same time caching the tokens for _Pragma when lexing it in macro argument pre-expansion mode.

When _Pragma is being lexed in macro argument pre-expansion mode, it caches the tokens so that it can avoid interpreting the pragma immediately (as the macro argument may not be used in the macro body), and then either backtracks over or commits these tokens. The problem is that, when we're backtracking/committing in such a scenario, there's already a previous backtracking position stored in `BacktrackPositions` (as we're skipping the function body), and this leads to a situation where the cached tokens from the pragma (like '(' 'string_literal' and ')') will remain in the cached tokens array incorrectly even after they're consumed (in the case of backtracking) or just ignored (in the case when they're committed). Furthermore, what makes it even worse, is that because of a previous backtracking position, the logic that deals with when should we call `ExitCachingLexMode` in `CachingLex` no longer works for us in this situation, and more tokens in the macro argument get cached, to the point where the EOF token that corresponds to the macro argument EOF is cached. This problem leads to all sorts of issues in code completion mode, where incorrect errors get presented and code completion completely fails to produce completion results.

Thanks for taking a look


Repository:
  rL LLVM

https://reviews.llvm.org/D28772

Files:
  include/clang/Lex/Preprocessor.h
  lib/Lex/PPCaching.cpp
  test/CodeCompletion/pragma-macro-token-caching.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28772.84561.patch
Type: text/x-patch
Size: 4229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170116/837d919b/attachment-0001.bin>


More information about the cfe-commits mailing list