11// Copyright (c) Microsoft. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ using Microsoft . CodeAnalysis . Sarif ;
45using Microsoft . VisualStudio . CodeAnalysis . CodeQL . Exceptions ;
56using Microsoft . VisualStudio . Shell ;
67using Newtonsoft . Json . Linq ;
@@ -43,7 +44,18 @@ public sealed class CodeQLRunner
4344 /// <summary>
4445 /// Gets or sets where the databases will be placed.
4546 /// </summary>
46- public string DatabaseDirectory { get ; set ; }
47+ public string DatabaseDirectory
48+ {
49+ get
50+ {
51+ if ( string . IsNullOrEmpty ( AnalysisDirectory ) )
52+ {
53+ throw new Exception ( "Analysis directory not set" ) ;
54+ }
55+ return Path . Combine ( AnalysisDirectory , "codeql_db" ) ;
56+ }
57+ set { }
58+ }
4759
4860 /// <summary>
4961 /// Gets or sets where codeql will be installed.
@@ -53,7 +65,10 @@ public sealed class CodeQLRunner
5365 /// <summary>
5466 /// Directory where analysis is being performed.
5567 /// </summary>
56- private string analysisDir ;
68+ public string AnalysisDirectory
69+ {
70+ get ; set ;
71+ }
5772
5873 /// <summary>
5974 /// Optional output windows pane.
@@ -155,12 +170,14 @@ public sealed class CodeQLRunner
155170
156171 public void Initialize ( string sourceDir = "" , string buildEnv = "" , Action < string > outputFunc = null )
157172 {
158- analysisDir = sourceDir ;
159- DatabaseDirectory = Path . Combine ( analysisDir , "codeql_db" ) ;
173+ if ( ! string . IsNullOrEmpty ( sourceDir ) )
174+ {
175+ AnalysisDirectory = sourceDir ;
176+ }
160177
161- if ( ! Directory . Exists ( analysisDir ) )
178+ if ( ! Directory . Exists ( AnalysisDirectory ) )
162179 {
163- throw new Exception ( "Analysis directory does not exist: " + analysisDir ) ;
180+ throw new Exception ( "Analysis directory does not exist: " + AnalysisDirectory ) ;
164181 }
165182 this . outputFunc = outputFunc ;
166183 this . buildEnv = buildEnv ;
@@ -378,7 +395,7 @@ public async Task<List<string>> FindQueriesAsync(string qlpack, bool queriesNSui
378395 string query = line . Replace ( "\" " , string . Empty ) . Replace ( "\\ \\ " , "/" ) . Replace ( "\\ " , "/" ) . Trim ( ',' ) . Trim ( ) ;
379396 if ( ! IsExclusion ( query ) )
380397 {
381- queries . Add ( query . Substring ( query . IndexOf ( "packages" ) ) ) ; // display packages from root of pack, not from root of filesystem
398+ queries . Add ( query ) ;
382399 }
383400 }
384401 }
@@ -494,7 +511,7 @@ private void ProcessExited(object sender, System.EventArgs e)
494511 /// <exception cref="Exception"> Exception </exception>
495512 public async Task < int > CountDBSourceCodeLinesAsync ( )
496513 {
497- string output = await RunCodeQLProcAsync ( "database print-baseline codeql_db" , workingDir : analysisDir ) ;
514+ string output = await RunCodeQLProcAsync ( "database print-baseline codeql_db" , workingDir : AnalysisDirectory ) ;
498515 foreach ( string line in output . Split ( Environment . NewLine . ToCharArray ( ) [ 0 ] ) )
499516 {
500517 if ( line . EndsWith ( "cpp." ) )
@@ -531,7 +548,7 @@ public async Task AddDBDiagInfoAsync(string projectDir, string message, int sour
531548 {
532549 throw new ArgumentException ( "Invalid severity level. Use 'error', 'warning', or 'note'." ) ;
533550 }
534- _ = await RunCodeQLProcAsync ( "database add-diagnostic --plaintext-message=\" " + message + "\" --source-id=" + sourceId . ToString ( ) + " --source-name=CodeqlVSExt" + " --severity=" + severity + " codeql_db" , workingDir : analysisDir ) ;
551+ _ = await RunCodeQLProcAsync ( "database add-diagnostic --plaintext-message=\" " + message + "\" --source-id=" + sourceId . ToString ( ) + " --source-name=CodeqlVSExt" + " --severity=" + severity + " codeql_db" , workingDir : AnalysisDirectory ) ;
535552 }
536553 catch ( Exception ex )
537554 {
@@ -545,7 +562,7 @@ public async Task AddDBDiagInfoAsync(string projectDir, string message, int sour
545562 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
546563 public async Task < string > GetDBDiagAsync ( )
547564 {
548- return await RunCodeQLProcAsync ( "database export-diagnostics .\\ codeql_db\\ --format=raw" , workingDir : analysisDir ) ;
565+ return await RunCodeQLProcAsync ( "database export-diagnostics .\\ codeql_db\\ --format=raw" , workingDir : AnalysisDirectory ) ;
549566 }
550567
551568 /// <summary>
@@ -646,7 +663,7 @@ public async Task GenerateDatabaseAsync(string buildCommand, CancellationToken c
646663 }
647664
648665 string strCmdText = string . Empty ;
649- string dbPath = System . IO . Path . Combine ( analysisDir , "codeql_db" ) ;
666+ string dbPath = System . IO . Path . Combine ( AnalysisDirectory , "codeql_db" ) ;
650667 string useThreads = string . IsNullOrWhiteSpace ( threads ) ? "" : " --threads=" + threads + " " ;
651668 string useRam = string . IsNullOrWhiteSpace ( ram ) ? "" : " --ram=" + ram + " " ;
652669
@@ -656,7 +673,7 @@ public async Task GenerateDatabaseAsync(string buildCommand, CancellationToken c
656673 "create" , "\" " + dbPath + "\" " ,
657674 "--force-overwrite" ,
658675 "--language=cpp" ,
659- "--source-root=" + "\" " + analysisDir + "\" " ,
676+ "--source-root=" + "\" " + AnalysisDirectory + "\" " ,
660677 "--command=" + "\" " + buildCommand + "\" " ,
661678 useThreads ,
662679 useRam
@@ -692,7 +709,7 @@ public async Task<string> RunCodeQLQuerySetAsync(string query, CancellationToken
692709 proccessExitedFunc = ProcessExited ;
693710 }
694711
695- string dbPath = Path . Combine ( analysisDir , "codeql_db" ) ;
712+ string dbPath = Path . Combine ( AnalysisDirectory , "codeql_db" ) ;
696713 if ( ! Directory . Exists ( dbPath ) )
697714 {
698715 throw new DatabaseNotFinalizedException ( "Database not created" ) ;
@@ -706,7 +723,7 @@ public async Task<string> RunCodeQLQuerySetAsync(string query, CancellationToken
706723 }
707724 }
708725
709- string resultsDir = Path . Combine ( analysisDir , ".sarif" ) ;
726+ string resultsDir = Path . Combine ( AnalysisDirectory , ".sarif" ) ;
710727 if ( ! Directory . Exists ( resultsDir ) )
711728 {
712729 _ = Directory . CreateDirectory ( resultsDir ) ;
0 commit comments