ljclang

FORK: A LuaJIT-based interface to libclang
git clone https://git.neptards.moe/neptards/ljclang.git
Log | Files | Refs

test.lua (1883B)


      1 #!/usr/bin/env luajit
      2 
      3 local arg = arg
      4 
      5 local assert = assert
      6 local print = print
      7 local require = require
      8 local tostring = tostring
      9 
     10 local string = require("string")
     11 local os = require("os")
     12 
     13 ----------
     14 
     15 assert(arg[1], "Usage: "..arg[0].." <filename> ...")
     16 
     17 local cl = require("ljclang")
     18 
     19 arg[0] = nil
     20 local tu = cl.createIndex():parse(arg, {"DetailedPreprocessingRecord"})
     21 
     22 -- NOTE: we don't need to keep the Index_t reference around, test this.
     23 collectgarbage()
     24 
     25 if (tu == nil) then
     26     print('TU is nil')
     27     os.exit(1)
     28 end
     29 
     30 local cur = tu:cursor()
     31 assert(cur==cur)
     32 assert(cur ~= nil)
     33 assert(cur:kindnum() == "CXCursor_TranslationUnit")
     34 assert(cur:haskind("TranslationUnit"))
     35 
     36 print("TU: "..cur:name()..", "..cur:displayName())
     37 local fn = arg[1]:gsub(".*/","")
     38 print(fn.." in TU: "..tu:file(fn)..", "..tu:file(arg[1]))
     39 
     40 local diags = tu:diagnostics()
     41 for i=1,#diags do
     42     local d = diags[i]
     43     print("diag "..i..": "..d.category..", "..d.text)
     44 end
     45 
     46 local V = cl.ChildVisitResult
     47 
     48 local ourtab = {}
     49 
     50 local visitor = cl.regCursorVisitor(
     51 function(cur, parent)
     52     ourtab[#ourtab+1] = cl.Cursor(cur)
     53 
     54     if (cur:haskind("EnumConstantDecl")) then
     55         print(string.format("%s: %d", cur:name(), cur:enumval()))
     56     end
     57 
     58     local isdef = (cur:haskind("FunctionDecl")) and cur:isDefinition()
     59 
     60 --    print(string.format("[%3d] %50s <- %s", tonumber(cur:kindnum()), tostring(cur), tostring(parent)))
     61     print(string.format("%3d [%12s%s] %50s <- %s", #ourtab, cur:kind(),
     62                         isdef and " (def)" or "", tostring(cur), tostring(parent)))
     63 
     64     if (cur:haskind("CXXMethod")) then
     65         print("("..cur:access()..")")
     66     end
     67 
     68     return V.Continue
     69 end)
     70 
     71 cur:children(visitor)
     72 
     73 local tab = cur:children()
     74 print("TU has "..#tab.." direct descendants:")
     75 for i=1,#tab do
     76     print(i..": "..tab[i]:kind()..": "..tab[i]:displayName())
     77     assert(tab[i] == ourtab[i])
     78 end