Skip to content

Commit 718f4cb

Browse files
committed
feat(debug): added preprocessor debug cli option
1 parent fd389be commit 718f4cb

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

fortls/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import sys
44
from multiprocessing import freeze_support
55

6-
from .debug import DebugError, debug_lsp, debug_parser, is_debug_mode
6+
from .debug import (
7+
DebugError,
8+
debug_lsp,
9+
debug_parser,
10+
debug_preprocessor,
11+
is_debug_mode,
12+
)
713
from .interface import cli
814
from .jsonrpc import JSONRPC2Connection, ReadWriter
915
from .langserver import LangServer
@@ -20,6 +26,9 @@ def main():
2026
if args.debug_parser:
2127
debug_parser(args)
2228

29+
elif args.debug_preproc:
30+
debug_preprocessor(args)
31+
2332
elif is_debug_mode(args):
2433
debug_lsp(args, vars(args))
2534

fortls/debug.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from __future__ import annotations
22

3+
import logging
34
import os
45
import pprint
6+
import sys
57

68
import json5
79

810
from .helper_functions import only_dirs, resolve_globs
911
from .jsonrpc import JSONRPC2Connection, ReadWriter, path_from_uri
1012
from .langserver import LangServer
11-
from .parsers.internal.parser import FortranFile
13+
from .parsers.internal.parser import FortranFile, preprocess_file
1214

1315

1416
class DebugError(Exception):
@@ -439,6 +441,56 @@ def debug_parser(args):
439441
separator()
440442

441443

444+
def debug_preprocessor(args):
445+
"""Debug the preprocessor of the Language Server
446+
Triggered by `--debug_preprocessor` option.
447+
448+
Parameters
449+
----------
450+
args : Namespace
451+
The arguments parsed from the `ArgumentParser`
452+
"""
453+
454+
def sep_lvl2(heading: str):
455+
print("\n" + "=" * 75 + f"\n{heading}\n" + "=" * 75)
456+
457+
print("\nTesting preprocessor")
458+
separator()
459+
460+
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, format="%(message)s")
461+
462+
file = args.debug_filepath
463+
ensure_file_accessible(file)
464+
with open(file, encoding="utf-8") as f:
465+
lines = f.readlines()
466+
467+
root = args.debug_rootpath if args.debug_rootpath else os.path.dirname(file)
468+
_, pp_defs, include_dirs = read_config(root, args.config)
469+
470+
sep_lvl2("Preprocessor Pass:")
471+
output, skips, defines, defs = preprocess_file(
472+
lines, file, pp_defs, include_dirs, debug=True
473+
)
474+
475+
sep_lvl2("Preprocessor Skipped Lines:")
476+
for line in skips:
477+
print(f" {line}")
478+
479+
sep_lvl2("Preprocessor Macros:")
480+
for key, value in defs.items():
481+
print(f" {key} = {value}")
482+
483+
sep_lvl2("Preprocessor Defines (#define):")
484+
for line in defines:
485+
print(f" {line}")
486+
487+
sep_lvl2("Preprocessor Final Output:")
488+
for line in output:
489+
print(rf" {line.rstrip()}")
490+
491+
separator()
492+
493+
442494
def ensure_file_accessible(filepath: str):
443495
"""Ensure the file exists and is accessible, raising an error if not."""
444496
if not os.path.isfile(filepath):

fortls/interface.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ def hide_opt(help: str) -> str:
333333
action="store_true",
334334
help=hide_opt("Test source code parser on specified file"),
335335
)
336+
group.add_argument(
337+
"--debug_preproc",
338+
action="store_true",
339+
help=hide_opt("Test source code preprocessor parser on specified file"),
340+
)
336341
group.add_argument(
337342
"--debug_hover",
338343
action="store_true",

0 commit comments

Comments
 (0)