forked from ishani/ClangVSx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVXConnect.cs
589 lines (518 loc) · 21.5 KB
/
CVXConnect.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* ClangVSx - Compiler Bridge for CLang in MS Visual Studio
* Harry Denholm, ishani.org 2011-2012
*
* https://github.com/ishani/ClangVSx
* http://www.ishani.org/web/articles/code/clangvsx/
*
* Released under LLVM Release License. See LICENSE.TXT for details.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.VisualStudio.VCProjectEngine;
using NEnhancer.Common;
using Thread = System.Threading.Thread;
namespace ClangVSx
{
public interface IBuildCancellation
{
bool ShouldCancelBuild();
}
/// <summary>The object for implementing an Add-in.</summary>
/// <seealso class='IDTExtensibility2' />
public class CVXConnect : IDTExtensibility2, IDTCommandTarget, IBuildCancellation
{
private readonly List<String> TemporaryFilesCreatedDuringThisSession = new List<String>();
private CommandBarEvents AnalyseCmdEvent;
private bool BuildInProgress;
private bool BuildCancelFlag;
private CommandBarEvents CompileCmdEvent;
private CommandBarEvents DasmCmdEvent;
private CommandBarEvents PreproCmdEvent;
private CommandBarButton RebuildActiveProjectButton;
private CommandBarButton RelinkButton;
private CommandBarButton CancelBuildButton;
private CommandBarButton SettingsButton;
// instance of the compiler bridge; this offers up the main worker functions that go off and compile files, projects, etc.
private ClangOps _CVXOps;
private AddIn _addInInstance;
private DTE2 _applicationObject;
private DTEHelper _dteHelper;
public bool ShouldCancelBuild()
{
return BuildCancelFlag;
}
#region Commands
private const string COMMAND_CLANG_SETTINGS_DLG = "ShowSettingsDlg";
private const string COMMAND_CLANG_REBUILD_ACTIVE = "RebuildActiveProject";
private const string COMMAND_CLANG_RELINK_ACTIVE = "RelinkActiveProject";
private const string COMMAND_CLANG_CANCEL_BUILD = "CancelBuild";
private string GetCommandFullName(string cmdName)
{
return "ClangVSx.CVXConnect." + cmdName;
}
private string GetCommandShortName(string fullCmdName)
{
return fullCmdName.Remove(0, ("ClangVSx.CVXConnect.").Length);
}
#endregion
/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public CVXConnect()
{
BuildInProgress = false;
BuildCancelFlag = false;
}
#region IDTCommandTarget Members
/// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
/// <param term='commandName'>The name of the command to determine state for.</param>
/// <param term='neededText'>Text that is needed for the command.</param>
/// <param term='status'>The state of the command in the user interface.</param>
/// <param term='commandText'>Text requested by the neededText parameter.</param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status,
ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName.StartsWith("ClangVSx.CVXConnect"))
{
status = vsCommandStatus.vsCommandStatusSupported;
if (commandName.EndsWith(COMMAND_CLANG_SETTINGS_DLG))
{
// don't change settings mid-build
if (!BuildInProgress)
status |= vsCommandStatus.vsCommandStatusEnabled;
}
else if (commandName.EndsWith(COMMAND_CLANG_REBUILD_ACTIVE))
{
if (!BuildInProgress && _applicationObject.Solution.Count != 0)
status |= vsCommandStatus.vsCommandStatusEnabled;
}
else if (commandName.EndsWith(COMMAND_CLANG_RELINK_ACTIVE))
{
if (!BuildInProgress && _applicationObject.Solution.Count != 0)
status |= vsCommandStatus.vsCommandStatusEnabled;
}
else if (commandName.EndsWith(COMMAND_CLANG_CANCEL_BUILD))
{
if (BuildInProgress && _applicationObject.Solution.Count != 0)
status |= vsCommandStatus.vsCommandStatusEnabled;
}
}
}
}
/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut,
ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
String cmd = GetCommandShortName(commandName);
switch (cmd)
{
case COMMAND_CLANG_SETTINGS_DLG:
{
handled = true;
ShowCVXSettingsDialog();
}
break;
case COMMAND_CLANG_CANCEL_BUILD:
{
handled = true;
BuildCancelFlag = true;
}
break;
case COMMAND_CLANG_RELINK_ACTIVE:
case COMMAND_CLANG_REBUILD_ACTIVE:
{
handled = true;
_applicationObject.Documents.SaveAll();
try
{
BuildCancelFlag = false;
// build a config options block, set the delegates for build events to toggle
// our local build-is-running variable that will disable all other Clang actions until it finishes
var pbc = new ClangOps.ProjectBuildConfig();
pbc.BuildBegun = (bool success) => { BuildInProgress = true; };
pbc.BuildFinished = (bool success) => { BuildInProgress = false; };
pbc.BuildShouldCancel = this;
pbc.JustLink = (cmd == COMMAND_CLANG_RELINK_ACTIVE);
// start the build on another thread so the output pane updates asynchronously
ParameterizedThreadStart buildDelegate =
_CVXOps.BuildActiveProject;
var newThread = new Thread(buildDelegate);
newThread.Start(pbc);
}
catch (Exception ex)
{
BuildInProgress = false;
MessageBox.Show(ex.Message, "ClangVSx - Project Build Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
break;
}
}
}
#endregion
#region IDTExtensibility2 Members
/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
/// <param term='application'>Root object of the host application.</param>
/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
/// <param term='addInInst'>Object representing this Add-in.</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
_dteHelper = new DTEHelper(_applicationObject, _addInInstance);
_CVXOps = new ClangOps(_applicationObject);
}
/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
}
/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref Array custom)
{
}
/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref Array custom)
{
var contextGUIDS = new object[] { };
var commands = (Commands2)_applicationObject.Commands;
var _commandBars =
((CommandBars)_applicationObject.CommandBars);
// add a top-level menu to stick some global options in
{
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar =
((CommandBars)_applicationObject.CommandBars)["MenuBar"];
int nenhancerPopupIndex = menuBarCommandBar.Controls.Count + 1;
var clangMenuRoot = menuBarCommandBar.Controls.Add(
MsoControlType.msoControlPopup,
Type.Missing,
Type.Missing,
nenhancerPopupIndex,
true) as CommandBarPopup;
clangMenuRoot.Caption = "Clang";
// open the settings dialog
{
_dteHelper.AddNamedCommand2(
COMMAND_CLANG_SETTINGS_DLG,
"Settings...",
"Settings...",
false,
0);
Command commandToAdd =
_applicationObject.Commands.Item(GetCommandFullName(COMMAND_CLANG_SETTINGS_DLG), 0);
SettingsButton =
commandToAdd.AddControl(clangMenuRoot.CommandBar, clangMenuRoot.CommandBar.Controls.Count + 1)
as CommandBarButton;
}
// get active vcproj, rebuild it with Clang
{
_dteHelper.AddNamedCommand2(
COMMAND_CLANG_REBUILD_ACTIVE,
"Rebuild Active Project",
"Rebuild Active Project",
false,
0);
Command commandToAdd =
_applicationObject.Commands.Item(GetCommandFullName(COMMAND_CLANG_REBUILD_ACTIVE), 0);
RebuildActiveProjectButton =
commandToAdd.AddControl(clangMenuRoot.CommandBar, clangMenuRoot.CommandBar.Controls.Count + 1)
as CommandBarButton;
}
{
_dteHelper.AddNamedCommand2(
COMMAND_CLANG_RELINK_ACTIVE,
"Relink",
"Relink",
false,
0);
Command commandToAdd =
_applicationObject.Commands.Item(GetCommandFullName(COMMAND_CLANG_RELINK_ACTIVE), 0);
RelinkButton =
commandToAdd.AddControl(clangMenuRoot.CommandBar, clangMenuRoot.CommandBar.Controls.Count + 1)
as CommandBarButton;
}
{
_dteHelper.AddNamedCommand2(
COMMAND_CLANG_CANCEL_BUILD,
"Cancel Build",
"Cancel Build",
false,
0);
Command commandToAdd =
_applicationObject.Commands.Item(GetCommandFullName(COMMAND_CLANG_CANCEL_BUILD), 0);
CancelBuildButton =
commandToAdd.AddControl(clangMenuRoot.CommandBar, clangMenuRoot.CommandBar.Controls.Count + 1)
as CommandBarButton;
}
}
// add a compile-this-file option to the editor window
{
Microsoft.VisualStudio.CommandBars.CommandBar codeWinCommandBar =
_dteHelper.GetCommandBarByName("Code Window");
int pmPopupIndex = codeWinCommandBar.Controls.Count + 1;
var pmPopup = codeWinCommandBar.Controls.Add(
MsoControlType.msoControlPopup,
Type.Missing,
Type.Missing,
pmPopupIndex,
true) as CommandBarPopup;
pmPopup.Caption = "Clang Compiler";
CommandBarButton saveAndCompileCmd = _dteHelper.AddButtonToPopup(
pmPopup,
pmPopup.Controls.Count + 1,
"Compile",
"Compile");
CompileCmdEvent = _applicationObject.Events.get_CommandBarEvents(saveAndCompileCmd) as CommandBarEvents;
CompileCmdEvent.Click += cvxCompileFile_menuop;
CommandBarButton saveAndAnalyseCmd = _dteHelper.AddButtonToPopup(
pmPopup,
pmPopup.Controls.Count + 1,
"Run Static Analysis",
"Run Static Analysis");
AnalyseCmdEvent = _applicationObject.Events.get_CommandBarEvents(saveAndAnalyseCmd) as CommandBarEvents;
AnalyseCmdEvent.Click += cvxAnalyseFile_menuop;
CommandBarButton saveAndDasmCmd = _dteHelper.AddButtonToPopup(
pmPopup,
pmPopup.Controls.Count + 1,
"View Disassembly (LLVM)",
"View Disassembly (LLVM)");
DasmCmdEvent = _applicationObject.Events.get_CommandBarEvents(saveAndDasmCmd) as CommandBarEvents;
DasmCmdEvent.Click += cvxDasmnFile_menuop;
CommandBarButton dasmAndPreproCmd = _dteHelper.AddButtonToPopup(
pmPopup,
pmPopup.Controls.Count + 1,
"View Preprocessor Result",
"View Preprocessor Result");
PreproCmdEvent = _applicationObject.Events.get_CommandBarEvents(dasmAndPreproCmd) as CommandBarEvents;
PreproCmdEvent.Click += cvxPreProFile_menuop;
}
}
/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref Array custom)
{
// try and be tidy!
foreach (String tempF in TemporaryFilesCreatedDuringThisSession)
{
if (File.Exists(tempF))
File.Delete(tempF);
}
}
#endregion
internal void ReportBuildInProgress()
{
MessageBox.Show("The Clang compiler is already in use, please wait.", "ClangVSx", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
/// <summary>
/// get the active code file and compile it
/// </summary>
protected void cvxCompileFile_menuop(object CommandBarControlObj, ref bool handled, ref bool cancelDefault)
{
if (BuildInProgress)
{
ReportBuildInProgress();
return;
}
VCFile vcFile;
VCProject vcProject;
VCConfiguration vcCfg;
if (GetActiveVCFile(out vcFile, out vcProject, out vcCfg))
{
_CVXOps.CompileSingleFile(vcFile, vcProject, vcCfg);
}
else
{
MessageBox.Show(
"Clang cannot compile files of this type / unrecognized code file (" +
_applicationObject.ActiveDocument.Language + ")", "ClangVSx");
}
}
/// <summary>
/// ask clang to emit assembly listing for the given file, then pop that result open in VS
/// </summary>
protected void cvxDasmnFile_menuop(object CommandBarControlObj, ref bool handled, ref bool cancelDefault)
{
if (BuildInProgress)
{
ReportBuildInProgress();
return;
}
VCFile vcFile;
VCProject vcProject;
VCConfiguration vcCfg;
if (GetActiveVCFile(out vcFile, out vcProject, out vcCfg))
{
String dasmOut = Path.GetTempFileName();
TemporaryFilesCreatedDuringThisSession.Add(dasmOut);
if (_CVXOps.CompileSingleFile(vcFile, vcProject, vcCfg,
String.Format("-save-temps -S -emit-llvm -o\"{0}\"", dasmOut)))
{
_applicationObject.ItemOperations.OpenFile(dasmOut, "{8E7B96A8-E33D-11D0-A6D5-00C04FB67F6A}");
}
}
else
{
MessageBox.Show(
"Clang cannot compile files of this type / unrecognized code file (" +
_applicationObject.ActiveDocument.Language + ")", "ClangVSx");
}
}
/// <summary>
/// dump the preprocessor and native-assembly format listing
/// </summary>
protected void cvxPreProFile_menuop(object CommandBarControlObj, ref bool handled, ref bool cancelDefault)
{
if (BuildInProgress)
{
ReportBuildInProgress();
return;
}
VCFile vcFile;
VCProject vcProject;
VCConfiguration vcCfg;
if (GetActiveVCFile(out vcFile, out vcProject, out vcCfg))
{
String ppOut = Path.GetTempFileName();
TemporaryFilesCreatedDuringThisSession.Add(ppOut);
if (_CVXOps.CompileSingleFile(vcFile, vcProject, vcCfg, String.Format("-save-temps -E -o\"{0}\"", ppOut)))
{
_applicationObject.ItemOperations.OpenFile(ppOut, "{8E7B96A8-E33D-11D0-A6D5-00C04FB67F6A}");
}
}
else
{
MessageBox.Show(
"Clang cannot compile files of this type / unrecognized code file (" +
_applicationObject.ActiveDocument.Language + ")", "ClangVSx");
}
}
/// <summary>
/// get the active code file and compile it, adding static analyzer arguments to the pile
/// </summary>
protected void cvxAnalyseFile_menuop(object CommandBarControlObj, ref bool handled, ref bool cancelDefault)
{
if (BuildInProgress)
{
ReportBuildInProgress();
return;
}
VCFile vcFile;
VCProject vcProject;
VCConfiguration vcCfg;
if (GetActiveVCFile(out vcFile, out vcProject, out vcCfg))
{
_CVXOps.CompileSingleFile(vcFile, vcProject, vcCfg, "--analyze -Xclang -analyzer-stats --analyzer-output text");
}
else
{
MessageBox.Show(
"Clang cannot analyse files of this type / unrecognized code file (" +
_applicationObject.ActiveDocument.Language + ")", "ClangVSx");
}
}
/// <summary>
/// Get the active code file, project and configuration
/// </summary>
/// <returns>true if we have found an active C/C++ document</returns>
private bool GetActiveVCFile(out VCFile vcFile, out VCProject vcProject, out VCConfiguration vcCfg)
{
vcFile = null;
vcProject = null;
vcCfg = null;
if (_applicationObject.ActiveDocument != null)
{
// GUID equates to 'code file' as far as I can make out
if (_applicationObject.ActiveDocument.Kind == "{8E7B96A8-E33D-11D0-A6D5-00C04FB67F6A}" &&
_applicationObject.ActiveDocument.Language == "C/C++")
{
// GUID equates to physical file on disk [http://msdn.microsoft.com/en-us/library/z4bcch80(VS.80).aspx]
if (_applicationObject.ActiveDocument.ProjectItem.Kind == "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}")
{
// leap of faith
vcFile = (VCFile)_applicationObject.ActiveDocument.ProjectItem.Object;
vcProject = (VCProject)vcFile.project;
if (vcFile.FileType != eFileType.eFileTypeCppCode)
return false;
// save the file (should be optional!)
if (!_applicationObject.ActiveDocument.Saved)
_applicationObject.ActiveDocument.Save(vcFile.FullPath);
// get current configuration to pass to the bridge
Configuration cfg =
_applicationObject.ActiveDocument.ProjectItem.ConfigurationManager.ActiveConfiguration;
try
{
var cfgArray = (IVCCollection)vcProject.Configurations;
foreach (VCConfiguration vcr in cfgArray)
{
if (vcr.ConfigurationName == cfg.ConfigurationName &&
vcr.Platform.Name == cfg.PlatformName)
{
vcCfg = vcr;
}
}
}
catch (Exception)
{
return false;
}
return true;
}
}
}
return false;
}
/// <summary>
/// display our settings panel
/// </summary>
private void ShowCVXSettingsDialog()
{
var wW = new IWinWrapper();
wW.mHandle = _applicationObject.MainWindow.HWnd;
var dlg = new CVXSettings();
dlg.ShowDialog(wW);
}
#region Nested type: IWinWrapper
/// <summary>
/// workaround for providing IWin32Window interface for IntPtr HWNDs
/// </summary>
private class IWinWrapper : IWin32Window
{
public int mHandle;
#region IWin32Window Members
public IntPtr Handle
{
get { return (IntPtr)mHandle; }
}
#endregion
}
#endregion
}
}