Skip to content

Commit

Permalink
[C++/ObjC] Fix: UPPER_CASE return types on separate line of function (#…
Browse files Browse the repository at this point in the history
…1559)

Commit db206f0 introduced a workaround for macros on their own line, but had
the side-effect of not scoping function definition with an all-upper-case
identifier return type on a separate line as entity.name.function anymore.

This commit makes sure to check for this case.
  • Loading branch information
rwols authored and wbond committed Jul 9, 2018
1 parent 0122315 commit 37e29ac
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 8 deletions.
25 changes: 21 additions & 4 deletions C++/C++.sublime-syntax
Expand Up @@ -20,9 +20,9 @@ file_extensions:
first_line_match: '-\*- C\+\+ -\*-'
scope: source.c++
variables:
identifier: '\b[[:alpha:]_][[:alnum:]_]*\b'
identifier: \b[[:alpha:]_][[:alnum:]_]*\b # upper and lowercase
macro_identifier: \b[[:upper:]_][[:upper:][:digit:]_]{2,}\b # only uppercase, at least 3 chars
path_lookahead: '(?:::\s*)?(?:{{identifier}}\s*::\s*)*(?:template\s+)?{{identifier}}'
macro_identifier: '\b[[:upper:]_][[:upper:][:digit:]_]+\b'
operator_method_name: '\boperator\s*(?:[-+*/%^&|~!=<>]|[-+*/%^&|=!<>]=|<<=?|>>=?|&&|\|\||\+\+|--|,|->\*?|\(\)|\[\]|""\s*{{identifier}})'
casts: 'const_cast|dynamic_cast|reinterpret_cast|static_cast'
operator_keywords: 'and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|xor|xor_eq|noexcept'
Expand Down Expand Up @@ -1447,7 +1447,7 @@ contexts:
preprocessor-data-structures:
- include: preprocessor-rule-enabled-data-structures
- include: preprocessor-rule-disabled-data-structures
- include: scope:source.c#preprocessor-practical-workarounds
- include: preprocessor-practical-workarounds

preprocessor-rule-disabled-data-structures:
- match: ^\s*((#if)\s+(0))\b
Expand Down Expand Up @@ -1899,6 +1899,23 @@ contexts:
scope: punctuation.section.block.end.c++
- include: expressions

preprocessor-practical-workarounds:
- include: preprocessor-convention-ignore-uppercase-ident-lines
- include: scope:source.c#preprocessor-convention-ignore-uppercase-calls-without-semicolon

preprocessor-convention-ignore-uppercase-ident-lines:
- match: ^(\s*{{macro_identifier}})+\s*$
scope: meta.assumed-macro.c++
push:
# It's possible that we are dealing with a function return type on its own line, and the
# name of the function is on the subsequent line.
- match: '(?={{path_lookahead}}({{generic_lookahead}}({{path_lookahead}})?)\s*\()'
set: [function-definition-params, global-function-identifier-generic]
- match: '(?={{path_lookahead}}\s*\()'
set: [function-definition-params, global-function-identifier]
- match: ^
pop: true

preprocessor-other:
- match: ^\s*(#\s*(?:if|ifdef|ifndef|elif|else|line|pragma|undef))\b
captures:
Expand Down Expand Up @@ -1946,4 +1963,4 @@ contexts:
- match: '>'
scope: punctuation.definition.string.end.c++
pop: true
- include: scope:source.c#preprocessor-practical-workarounds
- include: preprocessor-practical-workarounds
10 changes: 10 additions & 0 deletions C++/C.sublime-syntax
Expand Up @@ -1119,6 +1119,16 @@ contexts:
preprocessor-convention-ignore-uppercase-ident-lines:
- match: ^(\s*{{macro_identifier}})+\s*$
scope: meta.assumed-macro.c
push:
# It's possible that we are dealing with a function return type on its own line, and the
# name of the function is on the subsequent line.
- match: \s*({{identifier}})(?=\s*\()
captures:
1: meta.function.c entity.name.function.c
set: function-definition-params
- match: ^
pop: true


preprocessor-convention-ignore-uppercase-calls-without-semicolon:
- match: ^\s*({{macro_identifier}})\s*(\()(?=[^)]*\)\s*$)
Expand Down
44 changes: 44 additions & 0 deletions C++/syntax_test_c.c
Expand Up @@ -111,6 +111,50 @@ bool still_C_code_here = true;
/* <- storage.type */
/* ^ constant.language */

FOOBAR
hello() {
/* <- meta.function entity.name.function */
return 0;
}

EFIAPI
UserStructCompare (
/* <- meta.function entity.name.function */
IN CONST VOID *UserStruct1,
IN CONST VOID *UserStruct2
)
{
const USER_STRUCT *CmpStruct1;
/* <- meta.block storage.modifier */

CmpStruct1 = UserStruct1;
return KeyCompare (&CmpStruct1->Key, UserStruct2);
/* <- meta.block keyword.control */
/* ^ meta.block meta.function-call variable.function */
}

LIB_RESULT
foo()
/* <- meta.function entity.name.function */
{
return LIB_SUCCESS;
}

LIB_RESULT bar()
/* ^ meta.function entity.name.function */
{
return LIB_SUCCESS;
}

THIS_IS_REALLY_JUST_A_MACRO_AND_NOT_A_RETURN_TYPE
/* <- meta.assumed-macro */

int main() {
/* <- storage.type */
/* ^ meta.function entity.name.function */
return 0;
}

#if 0
#ifdef moo
/* <- - keyword.control */
Expand Down
51 changes: 51 additions & 0 deletions C++/syntax_test_cpp.cpp
Expand Up @@ -58,6 +58,57 @@ int g(int x = 5 \
/* <- keyword.control.import.define */
/* ^ entity.name.constant */

FOOBAR
hello() {
/* <- meta.function entity.name.function */
return 0;
}

EFIAPI
UserStructCompare (
/* <- meta.function entity.name.function */
IN CONST VOID *UserStruct1,
IN CONST VOID *UserStruct2
)
{
const USER_STRUCT *CmpStruct1;
/* <- meta.block storage.modifier */

CmpStruct1 = UserStruct1;
return KeyCompare (&CmpStruct1->Key, UserStruct2);
/* <- meta.block keyword.control */
/* ^ meta.block meta.function-call variable.function */
}

LIB_RESULT
foo()
/* <- meta.function entity.name.function */
{
return LIB_SUCCESS;
}

LIB_RESULT bar()
/* ^ meta.function entity.name.function */
{
return LIB_SUCCESS;
}

THIS_IS_REALLY_JUST_A_MACRO_AND_NOT_A_RETURN_TYPE
/* <- meta.assumed-macro */

int main() {
/* <- storage.type */
/* ^ meta.function entity.name.function */
return 0;
}

// This is a method/function with the return type on a separate line and so should not be a
// constructor.
FOOLIB_RESULT
some_namespace::some_function(int a_parameter, double another_parameter) {
/* <- meta.function entity.name.function - entity.name.function.constructor */
return FOOLIB_SUCCESS;
}

#pragma foo(bar, \
"baz", \
Expand Down
8 changes: 4 additions & 4 deletions Objective-C/Objective-C++.sublime-syntax
Expand Up @@ -8,9 +8,9 @@ file_extensions:
- h
scope: source.objc++
variables:
identifier: '\b[[:alpha:]_][[:alnum:]_]*\b'
identifier: \b[[:alpha:]_][[:alnum:]_]*\b # upper and lowercase
macro_identifier: \b[[:upper:]_][[:upper:][:digit:]_]{2,}\b # only uppercase, at least 3 chars
path_lookahead: '(?:::\s*)?(?:{{identifier}}\s*::\s*)*(?:template\s+)?{{identifier}}'
macro_identifier: '\b[[:upper:]_][[:upper:][:digit:]_]+\b'
operator_method_name: '\boperator\s*(?:[-+*/%ˆ&|~!=<>]|[-+*/%^&|=!<>]=|<<=?|>>=?|&&|\|\||\+\+|--|,|->\*?|\(\)|\[\]|""\s*{{identifier}})'
casts: 'const_cast|dynamic_cast|reinterpret_cast|static_cast'
operator_keywords: 'and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|xor|xor_eq|noexcept'
Expand Down Expand Up @@ -1518,7 +1518,7 @@ contexts:
preprocessor-data-structures:
- include: preprocessor-rule-enabled-data-structures
- include: preprocessor-rule-disabled-data-structures
- include: scope:source.c#preprocessor-practical-workarounds
- include: scope:source.c++#preprocessor-practical-workarounds

preprocessor-rule-disabled-data-structures:
- match: ^\s*((#if)\s+(0))\b
Expand Down Expand Up @@ -2007,7 +2007,7 @@ contexts:
push:
- meta_scope: meta.preprocessor.import.objc++
- include: preprocessor-other-include-common
- include: scope:source.c#preprocessor-practical-workarounds
- include: scope:source.c++#preprocessor-practical-workarounds

preprocessor-other-include-common:
- include: scope:source.c#preprocessor-line-continuation
Expand Down
51 changes: 51 additions & 0 deletions Objective-C/syntax_test_objc++.mm
Expand Up @@ -58,6 +58,57 @@ int g(int x = 5 \
/* <- keyword.control.import.define */
/* ^ entity.name.constant */

FOOBAR
hello() {
/* <- meta.function entity.name.function */
return 0;
}

EFIAPI
UserStructCompare (
/* <- meta.function entity.name.function */
IN CONST VOID *UserStruct1,
IN CONST VOID *UserStruct2
)
{
const USER_STRUCT *CmpStruct1;
/* <- meta.block storage.modifier */

CmpStruct1 = UserStruct1;
return KeyCompare (&CmpStruct1->Key, UserStruct2);
/* <- meta.block keyword.control */
/* ^ meta.block meta.function-call variable.function */
}

LIB_RESULT
foo()
/* <- meta.function entity.name.function */
{
return LIB_SUCCESS;
}

LIB_RESULT bar()
/* ^ meta.function entity.name.function */
{
return LIB_SUCCESS;
}

THIS_IS_REALLY_JUST_A_MACRO_AND_NOT_A_RETURN_TYPE
/* <- meta.assumed-macro */

int main() {
/* <- storage.type */
/* ^ meta.function entity.name.function */
return 0;
}

// This is a method/function with the return type on a separate line and so should not be a
// constructor.
FOOLIB_RESULT
some_namespace::some_function(int a_parameter, double another_parameter) {
/* <- meta.function entity.name.function - entity.name.function.constructor */
return FOOLIB_SUCCESS;
}

#pragma foo(bar, \
"baz", \
Expand Down
44 changes: 44 additions & 0 deletions Objective-C/syntax_test_objc.m
Expand Up @@ -111,6 +111,50 @@ int f(int x, \
/* <- storage.type */
/* ^ constant.language */
FOOBAR
hello() {
/* <- meta.function entity.name.function */
return 0;
}
EFIAPI
UserStructCompare (
/* <- meta.function entity.name.function */
IN CONST VOID *UserStruct1,
IN CONST VOID *UserStruct2
)
{
const USER_STRUCT *CmpStruct1;
/* <- meta.block storage.modifier */
CmpStruct1 = UserStruct1;
return KeyCompare (&CmpStruct1->Key, UserStruct2);
/* <- meta.block keyword.control */
/* ^ meta.block meta.function-call variable.function */
}
LIB_RESULT
foo()
/* <- meta.function entity.name.function */
{
return LIB_SUCCESS;
}
LIB_RESULT bar()
/* ^ meta.function entity.name.function */
{
return LIB_SUCCESS;
}
THIS_IS_REALLY_JUST_A_MACRO_AND_NOT_A_RETURN_TYPE
/* <- meta.assumed-macro */
int main() {
/* <- storage.type */
/* ^ meta.function entity.name.function */
return 0;
}
#if 0
#ifdef moo
/* <- - keyword.control */
Expand Down

0 comments on commit 37e29ac

Please sign in to comment.