From 2caf08855f99bbf816708a1497512d16b631289a Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 6 Nov 2016 11:19:59 +0100 Subject: [PATCH] nimsuggest supports include files properly; added the compiler itself as a testcase --- compiler/modulegraphs.nim | 18 +++++++++++++++++- compiler/modules.nim | 1 + tools/nimsuggest/nimsuggest.nim | 10 ++++------ tools/nimsuggest/tests/tinclude.nim | 7 +++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 tools/nimsuggest/tests/tinclude.nim diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 2c16717894..9a3caa6632 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -25,7 +25,7 @@ ## - Its dependent module stays the same. ## -import ast, intsets +import ast, intsets, tables type ModuleGraph* = ref object @@ -34,6 +34,8 @@ type deps*: IntSet # the dependency graph or potentially its transitive closure. suggestMode*: bool # whether we are in nimsuggest mode or not. invalidTransitiveClosure: bool + inclToMod*: Table[int32, int32] # mapping of include file to the + # first module that included it {.this: g.} @@ -42,11 +44,13 @@ proc newModuleGraph*(): ModuleGraph = initStrTable(result.packageSyms) result.deps = initIntSet() result.modules = @[] + result.inclToMod = initTable[int32, int32]() proc resetAllModules*(g: ModuleGraph) = initStrTable(packageSyms) deps = initIntSet() modules = @[] + inclToMod = initTable[int32, int32]() proc getModule*(g: ModuleGraph; fileIdx: int32): PSym = if fileIdx >= 0 and fileIdx < modules.len: @@ -61,6 +65,18 @@ proc addDep*(g: ModuleGraph; m: PSym, dep: int32) = # this improve efficiency quite a lot: invalidTransitiveClosure = true +proc addIncludeDep*(g: ModuleGraph; module, includeFile: int32) = + discard hasKeyOrPut(inclToMod, includeFile, module) + +proc parentModule*(g: ModuleGraph; fileIdx: int32): int32 = + ## returns 'fileIdx' if the file belonging to this index is + ## directly used as a module or else the module that first + ## references this include file. + if fileIdx >= 0 and fileIdx < modules.len and modules[fileIdx] != nil: + result = fileIdx + else: + result = inclToMod.getOrDefault(fileIdx) + proc transitiveClosure(g: var IntSet; n: int) = # warshall's algorithm for k in 0..= 2: graph.symFromInfo(gTrackPos) else: usageSym if u != nil: diff --git a/tools/nimsuggest/tests/tinclude.nim b/tools/nimsuggest/tests/tinclude.nim new file mode 100644 index 0000000000..77492d745c --- /dev/null +++ b/tools/nimsuggest/tests/tinclude.nim @@ -0,0 +1,7 @@ +discard """ +$nimsuggest --tester compiler/nim.nim +>def compiler/semexprs.nim:13:50 +def;;skType;;ast.PSym;;PSym;;*ast.nim;;668;;2;;"";;100 +>def compiler/semexprs.nim:13:50 +def;;skType;;ast.PSym;;PSym;;*ast.nim;;668;;2;;"";;100 +"""