atom / language-c

C support in Atom
Other
116 stars 154 forks source link

Stringize and concatenate macros (# and ##) are not highlighted properly #318

Open prati0100 opened 5 years ago

prati0100 commented 5 years ago

Prerequisites

Description

When using # and ## operators, the tree-sitter parser incorrectly highlights them like #defines, which is not consistent with the part preceeding them. This was also partly a problem without tree-sitter, but only for using ## in function name.

With tree-sitter: atom_with_tree_sitter

Without tree-sitter: atom_without_tree_sitter

In the images, name is being highlighted incorrectly whenever the # and ## operators are used. taskqueue_define_##name should instead be highlighted like a function name. #name should be highlighted like a variable name (or a string? I'll let you guys decide).

Steps to Reproduce

  1. Use the stringify and token concatenation preprocessor operators.

edit by @rsese to add code for copy/paste and more screenshots

/*
 * Define and initialize...
 */
#define TASKQUEUE_DEFINE(name, enqueue, context, init)                     \
                                                                           \
struct taskqueue *taskqueue_##name;                                        \
                                                                           \
static void                                                                \
taskqueue_define_##name(void *arg)                                         \
{                                                                          \
  taskqueue_##name =                                                       \
    taskqueue_create(#name, M_WAITOK, (enqueue), (context));               \
  init;                                                                    \
}                                                                          \

Tree-sitter:

tree-sitter

Without:

tree-sitter-disabled

Reproduces how often: 100%

Versions

atom --version: Atom : 1.34.0 Electron: 2.0.16 Chrome : 61.0.3163.100 Node : 8.9.3

apm --version: apm 2.1.3 npm 6.2.0 node 8.9.3 x64 atom 1.34.0 python 2.7.15rc1 git 2.17.1

OS: Ubuntu 18.04

rsese commented 5 years ago

Thanks for the report! Reproduce with 1.34.0 on macOS 10.12.6:

Tree-sitter:

tree-sitter

Without:

tree-sitter-disabled