Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from abusalimov/24-compound-def
Browse files Browse the repository at this point in the history
Improve detection of definitions of compounds (enum / struct / union)
  • Loading branch information
abusalimov committed Nov 7, 2015
2 parents 8c9585e + 4a0fb3e commit 3f4fd9c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
23 changes: 12 additions & 11 deletions C Improved.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
<dict> <key>include</key> <string>#special_block</string> </dict>

<dict> <key>include</key> <string>#typedef</string> </dict>
<dict> <key>include</key> <string>#lex</string> </dict>

<dict> <key>include</key> <string>#type</string> </dict>

<dict> <key>include</key> <string>#lex</string> </dict>

<dict> <key>include</key> <string>#support-macro</string> </dict>
<dict> <key>include</key> <string>#function</string> </dict>
<dict> <key>include</key> <string>#support</string> </dict>
Expand Down Expand Up @@ -102,10 +102,10 @@
<key>patterns</key>
<array>
<dict> <key>include</key> <string>#typedef</string> </dict>
<dict> <key>include</key> <string>#lex</string> </dict>

<dict> <key>include</key> <string>#type</string> </dict>

<dict> <key>include</key> <string>#lex</string> </dict>

<dict> <key>include</key> <string>#call</string> </dict>
<dict> <key>include</key> <string>#support</string> </dict>

Expand All @@ -131,13 +131,13 @@
<key>name</key> <string>meta.parens.c</string>
<key>patterns</key>
<array>
<dict> <key>include</key> <string>#type</string> </dict>

<dict> <key>include</key> <string>#lex</string> </dict>

<dict> <key>include</key> <string>#call</string> </dict>
<dict> <key>include</key> <string>#support</string> </dict>

<dict> <key>include</key> <string>#type</string> </dict>

<dict> <key>include</key> <string>#block</string> </dict>
<dict> <key>include</key> <string>#parens</string> </dict>
</array>
Expand Down Expand Up @@ -210,20 +210,21 @@
<string>(?x)
(?: (?!\s*(?:[A-Za-z_({]|/[/*]|$))
| (?= \s*\b(?: [A-Za-z_]\w*+ ) (?= \s* [\[;] ) )
| (?&lt;!\})(?= \s*
| (?&lt;!\})(?=
# Prefer function definition over an attribute defined
# through a macro, unless a block has been seen. That is:
# struct __packed __aligned(16) foo {...}; - function __aligned
# struct foo {...} __packed __aligned(16); - structure foo
(?! \b__attribute__\b )
(?! \s* \b__attribute__\b )
(?: (?&lt;! (?&lt;!\w) new
(?: ^
| (?&lt;! (?&lt;!\w) new
| (?&lt;!\w) (?:else|enum) | (?&lt;!\w) (?:class|union)
| (?&lt;!\w) (?:struct|return|sizeof|typeof)
| (?&lt;!\w) __typeof | (?&lt;!\w) __typeof__ )
(?&lt;= \w ) \s
| # or type modifier / closing bracket before name
| # or type modifier / closing bracket before name
(?&lt;= [^&amp;]&amp; | [*&gt;)}\]] ) ) \s*
(?: [A-Za-z_]\w*+ | ::[^:] )++
Expand All @@ -248,7 +249,7 @@
<dict> <key>include</key> <string>#block</string> </dict>
<dict> <key>include</key> <string>#parens</string> </dict>
<dict>
<key>match</key> <string>\b([A-Za-z_]\w*+)(?=(?:\s|/\*.*?\*/)*+\{)</string>
<key>match</key> <string>\b([A-Za-z_]\w*+)(?=(?:\s|/\*.*?\*/)*+(?:\{|(//.*)?\\?$))</string>
<key>captures</key>
<dict>
<key>1</key> <dict> <key>name</key> <string>entity.name.type.class.c entity.name.class.c</string> </dict>
Expand Down
41 changes: 41 additions & 0 deletions tests/test_struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
struct name { };

struct name {
};
struct /* comment */ name /* comment */ { };

struct name
{ };
struct name \
{ };
struct /* comment */ name // comment
{ };

/* type-definition inside parens and block. */
void function(struct name {} arg);
void function(struct /* comment */ name /* comment */ { } arg) {
struct name { };
struct name
{ };
struct name \
{ };
struct /* comment */ name // comment
{ };
}

struct name *
create_struct(void) { }


/* FALSE POSITIVES */

struct name
create_struct(void);

struct name
create_struct(void) { }

struct name // the compound meta block shouldn't span
create_struct(void) { }

struct __packed __aligned(16) name { }; // function __aligned

0 comments on commit 3f4fd9c

Please sign in to comment.