-
Notifications
You must be signed in to change notification settings - Fork 1
/
LLDBSharpGen.cs
154 lines (126 loc) · 5.85 KB
/
LLDBSharpGen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using CppSharp;
using CppSharp.AST;
using CppSharp.Passes;
using System;
using System.IO;
using System.Linq;
namespace Mono
{
class LLDBSharpGen : ILibrary
{
const string LLDBPath = @"G:\llvm-new\tools\lldb\include";
public void Setup(Driver driver)
{
var options = driver.Options;
options.LibraryName = "lldb";
options.Verbose = false;
options.GenerateLibraryNamespace = false;
if (Platform.IsMacOS)
options.TargetTriple = "i686-apple-darwin12.4.0";
else if (Platform.IsWindows)
options.TargetTriple = "i686-pc-windows-msvc";
var outputDir = GetSourceDirectory("LLDBSharp");
options.OutputDir = Path.GetFullPath(Path.Combine(outputDir, options.TargetTriple));
options.addIncludeDirs(Path.GetFullPath(LLDBPath));
options.Headers.Add("lldb/lldb-defines.h");
options.Headers.Add("lldb/API/LLDB.h");
options.GenerateProperties = true;
options.CompileCode = false;
}
public void SetupPasses(Driver driver)
{
driver.TranslationUnitPasses.RemovePrefix("SB");
driver.AddTranslationUnitPass(new FixEnumsName());
driver.AddTranslationUnitPass(new FixParameterUsageFromComments());
}
public void Preprocess(Driver driver, ASTContext ctx)
{
ctx.IgnoreHeadersWithName("lldb/lldb-forward.h");
ctx.IgnoreHeadersWithName("lldb/API/SBDefines.h");
ctx.SetMethodParameterUsage("lldb::SBTarget", "Launch", 2, 2, ParameterUsage.Out);
ctx.SetMethodParameterUsage("lldb::SBTarget", "Launch", 10, 10, ParameterUsage.Out);
ctx.RemoveEnumItemsPrefix("lldb::StateType", "eState");
ctx.RemoveEnumItemsPrefix("lldb::LaunchFlags", "eLaunchFlag");
ctx.RemoveEnumItemsPrefix("lldb::RunMode", "e");
ctx.RemoveEnumItemsPrefix("lldb::ExpressionResults", "eExpression");
ctx.RemoveEnumItemsPrefix("lldb::SymbolContextItem", "eSymbolContext");
ctx.RemoveEnumItemsPrefix("lldb::InputReaderAction", "eInputReader");
ctx.RemoveEnumItemsPrefix("lldb::DynamicValueType", "e");
ctx.RemoveEnumItemsPrefix("lldb::InputReaderAction", "eInputReader");
ctx.RemoveEnumItemsPrefix("lldb::AccessType", "eAccess");
ctx.RemoveEnumItemsPrefix("lldb::CommandArgumentType", "eArgType");
ctx.RemoveEnumItemsPrefix("lldb::EmulateInstructionOptions", "eEmulateInstructionOption");
ctx.RemoveEnumItemsPrefix("lldb::TypeOptions", "eTypeOption");
ctx.RemoveEnumItemsPrefix("lldb::FrameComparison", "eFrameCompare");
ctx.RemoveEnumItemsPrefix("lldb::ExpressionEvaluationPhase", "eExpressionEvaluation");
ctx.RemoveEnumItemsPrefix("lldb::TypeFlags", "eType");
ctx.RemoveEnumItemsPrefix("lldb::CommandFlags", "eCommand");
ctx.RemoveEnumItemsPrefix("lldb::TypeSummaryCapping", "eTypeSummary");
ctx.SetNameOfClassMethod("lldb::SBError", "GetError", "GetErrorCode");
ctx.SetNameOfClassMethod("lldb::SBValue", "GetValue", "GetValueAsString");
ctx.GenerateEnumFromMacros("BreakpointId", "LLDB_BREAK_*", "LLDB_INVALID_BREAK_ID",
"LLDB_DEFAULT_BREAK_SIZE");
ctx.GenerateEnumFromMacros("WatchpointId", "LLDB_WATCH_*", "LLDB_INVALID_WATCH_ID");
ctx.GenerateEnumFromMacros("GenericRegister", "LLDB_REGNUM_GENERIC_*");
ctx.GenerateEnumFromMacros("InvalidValue", "LLDB_INVALID_*");
ctx.GenerateEnumFromMacros("CPUType", "LLDB_ARCH_*", "LLDB_INVALID_CPUTYPE");
ctx.GenerateEnumFromMacros("OptionSet", "LDB_OPT_SET_*", "LLDB_MAX_NUM_OPTION_SETS");
ctx.RenameNamespace("lldb", "LLDB");
}
public void Postprocess(Driver driver, ASTContext ctx)
{
ctx.SetMethodParameterUsage("LLDB::Target", "ReadMemory", 4, 2, ParameterUsage.In);
}
static string GetSourceDirectory(string dir)
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (directory != null)
{
var path = Path.Combine(directory.FullName, dir);
if (Directory.Exists(path) &&
Directory.Exists(Path.Combine(directory.FullName, "tests")))
return path;
directory = directory.Parent;
}
throw new Exception("Could not find build directory: " + dir);
}
static class Program
{
public static void Main(string[] args)
{
ConsoleDriver.Run(new LLDBSharpGen());
}
}
}
public static class ASTExtensions
{
public static void RemoveEnumItemsPrefix(this ASTContext ctx, string enumName, string prefix)
{
var @enum = ctx.FindEnum(enumName).FirstOrDefault();
if (@enum == null)
return;
if (@enum.Items.Count == 0)
return;
foreach (var item in @enum.Items)
{
if (!item.Name.StartsWith(prefix))
continue;
item.Name = item.Name.Substring(prefix.Length);
}
}
}
public class FixEnumsName : TranslationUnitPass
{
public override bool VisitEnumDecl(Enumeration @enum)
{
if (@enum.Items.Count == 0)
return base.VisitEnumDecl(@enum);
string enumPrefix = "e" + @enum.Name;
if (!@enum.Items[0].Name.StartsWith(enumPrefix))
return base.VisitEnumDecl(@enum);
foreach (var item in @enum.Items)
item.Name = item.Name.Substring(enumPrefix.Length);
return base.VisitEnumDecl(@enum);
}
}
}