Preprocessor variables not accounted for in diagnostics.
kc9jud opened this issue · 4 comments
Describe the bug
Preprocessor definitions do not seem to be applied to declarations correctly.
To Reproduce
A minimal example:
program mbwf2smwf
implicit none
character(LEN=MAX_PATHLENGTH) :: a, b='ffff '
a = TRIM(b)
end program mbwf2smwfWithin .fortls, we have:
{
"pp_defs": {"MAX_PATHLENGTH": "256"}
}Using VS Code, this gives an error squiggle under MAX_PATHLENGTH in the declaration, as well as error squiggles on a and b in a = TRIM(b):

It also gives an error when hovering:

Somehow, there are conflicting messages in the hover, where there is simultaneously no IMPLICIT type as well as the correctly deduced type CHARACTER(LEN=256):

Here is the output of adding --debug_log to the startup options for fortls. I have not been able to reproduce the messages from the command line.
REQUEST 4 textDocument/hover
SEND {"jsonrpc":"2.0","id":4,"result":{"contents":[{"language":"fortran90","value":"#define MAX_PATHLENGTH 256"}]}}
RECV {"jsonrpc":"2.0","id":5,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"range":{"start":{"line":2,"character":17},"end":{"line":2,"character":17}},"context":{"diagnostics":[{"range":{"start":{"line":2,"character":17},"end":{"line":2,"character":17}},"message":"Scalar INTEGER expression expected at (1)","severity":1}],"only":["quickfix"],"triggerKind":1}}}
REQUEST 5 textDocument/codeAction
SEND {"jsonrpc":"2.0","id":5,"result":null}
RECV {"jsonrpc":"2.0","id":6,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"position":{"line":3,"character":11}}}
REQUEST 6 textDocument/hover
SEND {"jsonrpc":"2.0","id":6,"result":{"contents":["TRIM(STRING) removes trailing blank characters of a string."]}}
RECV {"jsonrpc":"2.0","id":7,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"position":{"line":3,"character":13}}}
REQUEST 7 textDocument/hover
SEND {"jsonrpc":"2.0","id":7,"result":{"contents":[{"language":"fortran90","value":"CHARACTER(LEN=256)"}]}}
RECV {"jsonrpc":"2.0","id":8,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":13}},"context":{"diagnostics":[{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":13}},"message":"Symbol 'b' at (1) has no IMPLICIT type","severity":1}],"only":["quickfix"],"triggerKind":1}}}
REQUEST 8 textDocument/codeAction
SEND {"jsonrpc":"2.0","id":8,"result":null}
RECV {"jsonrpc":"2.0","id":9,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"position":{"line":3,"character":4}}}
REQUEST 9 textDocument/hover
SEND {"jsonrpc":"2.0","id":9,"result":{"contents":[{"language":"fortran90","value":"CHARACTER(LEN=256)"}]}}
RECV {"jsonrpc":"2.0","id":10,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"position":{"line":3,"character":3}}}
REQUEST 10 textDocument/hover
SEND {"jsonrpc":"2.0","id":10,"result":{"contents":[{"language":"fortran90","value":"CHARACTER(LEN=256)"}]}}
RECV {"jsonrpc":"2.0","id":11,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///home/pjfasano/Research/code/mfdn-transitions/src/mbwf2smwf.f90"},"range":{"start":{"line":3,"character":4},"end":{"line":3,"character":4}},"context":{"diagnostics":[{"range":{"start":{"line":3,"character":4},"end":{"line":3,"character":4}},"message":"Symbol 'a' at (1) has no IMPLICIT type","severity":1}],"only":["quickfix"],"triggerKind":1}}}
REQUEST 11 textDocument/codeAction
SEND {"jsonrpc":"2.0","id":11,"result":null}
Expected behavior
The erroneous warnings about missing types and missing integer constants should not appear.
Setup information (please complete the following information):
- OS: Ubuntu 22.04
- Python Version: 3.10.12
- fortls Version: 2.13.0
- Code editor used: VS Code
- the Fortran extension for the code editor and its version: Modern Fortran v3.2.0
Configuration information (please complete the following information):
- See above.
- Defaults.
I understand the confusion, the diagnostics you are actually getting are from the linter, gfortran in this case, of the Modern Fortran extension. You need to specify the preprocessor flag, as a linter extra argument e.g. -DMAX_PATHLENGTH=256.
For more see https://github.com/fortran-lang/vscode-fortran-support#additional-linting-options.
P.S. I will be moving this issue and converting it to a Discussion under the https://github.com/fortran-lang/vscode-fortran-support repo where it belongs
@gnikit Ah, that explains the inconsistency! Also why I couldn't reproduce it on the command line...
Is there a way to specify those kinds of options in a config file? Or for the linter to parse .fortls?
Unfortunately not. I have been toying with the idea of merging the fortls and linter interfaces (in VS Code) but there are some difficulties associated with that. e.g. certain options should only be present to fortls and some others only to the linters.
You can however specify project vscode configuration settings, either a a workspace or just a normal project.