question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on conditional compilation preprocessor directive

See original GitHub issue
var a = new ArrayList()
{
    #if DEBUG
        Capacity = 3
    #else
        Capacity = 4
    #endif
};

Actual:

(compilation_unit [0, 8] - [7, 10]
  (global_statement [0, 8] - [7, 10]
    (local_declaration_statement [0, 8] - [7, 10]
      (variable_declaration [0, 8] - [7, 9]
        type: (implicit_type [0, 8] - [0, 11])
        (variable_declarator [0, 12] - [7, 9]
          (identifier [0, 12] - [0, 13])
          (equals_value_clause [0, 14] - [7, 9]
            (object_creation_expression [0, 16] - [7, 9]
              type: (identifier [0, 20] - [0, 29])
              arguments: (argument_list [0, 29] - [0, 31])
              initializer: (initializer_expression [1, 8] - [7, 9]
                (preprocessor_call [2, 12] - [2, 21]
                  (preprocessor_directive [2, 12] - [2, 15])
                  (identifier [2, 16] - [2, 21]))
                (assignment_expression [3, 16] - [5, 28]
                  left: (identifier [3, 16] - [3, 24])
                  (assignment_operator [3, 25] - [3, 26])
                  right: (assignment_expression [3, 27] - [5, 28]
                    left: (integer_literal [3, 27] - [3, 28])
                    (preprocessor_call [4, 12] - [4, 17]
                      (preprocessor_directive [4, 12] - [4, 17]))
                    (ERROR [5, 16] - [5, 24])
                    (assignment_operator [5, 25] - [5, 26])
                    right: (integer_literal [5, 27] - [5, 28])))
                (preprocessor_call [6, 12] - [6, 18]
                  (preprocessor_directive [6, 12] - [6, 18]))))))))))
foo.cs	0 ms	(ERROR [5, 16] - [5, 24])

Expected:

(compilation_unit [0, 8] - [8, 0]
  (global_statement [0, 8] - [7, 10]
    (local_declaration_statement [0, 8] - [7, 10]
      (variable_declaration [0, 8] - [7, 9]
        type: (implicit_type [0, 8] - [0, 11])
        (variable_declarator [0, 12] - [7, 9]
          (identifier [0, 12] - [0, 13])
          (equals_value_clause [0, 14] - [7, 9]
            (object_creation_expression [0, 16] - [7, 9]
              type: (identifier [0, 20] - [0, 29])
              arguments: (argument_list [0, 29] - [0, 31])
              initializer: (initializer_expression [1, 8] - [7, 9]
                (preprocessor_call [2, 12] - [2, 21]
                  (preprocessor_directive [2, 12] - [2, 15])
                  (identifier [2, 16] - [2, 21]))
                (assignment_expression [3, 16] - [3, 28]
                  left: (identifier [3, 16] - [3, 24])
                  (assignment_operator [3, 25] - [3, 26])
                  right: (integer_literal [3, 27] - [3, 28]))
                (preprocessor_call [4, 12] - [4, 17]
                  (preprocessor_directive [4, 12] - [4, 17]))
                (assignment_expression [5, 16] - [5, 28]
                  left: (identifier [5, 16] - [5, 24])
                  (assignment_operator [5, 25] - [5, 26])
                  right: (integer_literal [5, 27] - [5, 28]))
                (preprocessor_call [6, 12] - [6, 18]
                  (preprocessor_directive [6, 12] - [6, 18]))))))))))

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
damiengcommented, Jul 27, 2021

Yes, this is the same underlying cause and limitation. There is no way anything other than a compiler can definitively resolve that kind of fragmented condition - you would need to evaluate all the preprocessor conditions to know which paths are valid.

0reactions
damiengcommented, Jul 27, 2021

That said, it is my intention to adopt something similar to what the C grammar is doing that Max described above which will at least let us parse/recover even if we don’t definitively know what was intended (I’m just wrapping up a paid project and hope to have some time to devote to tree-sitter-c-sharp again in the next week or so)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Compilation Errors in Pl/SQL
Here's a list of common Pl/SQL Conditional Compilation errors, ... PLS-00177: '$IF' preprocessor directive does not end properly.
Read more >
C# preprocessor directives | Microsoft Learn
In this article. Nullable context; Conditional compilation; Defining symbols; Defining regions; Error and warning information; Pragmas.
Read more >
How do I show conditional error using C preprocessor during ...
This is just a way to use the C preprocessor #error directive for showing errors according to condition during compilation Like for example:...
Read more >
8.2 Conditional Compilation (#if, #ifdef, #ifndef, #else, #elif ...
They delimit blocks of program text that are compiled only if a specified condition is true. These directives can be nested. The program...
Read more >
What is the #error directive in C++? - Educative.io
#error is a Preprocessor Directive that has the following structure: ... #error directive is normally used to throw an error during conditional compilation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found