Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Add options to cccheck to find source in a different location #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Foxtrot/Foxtrot/Documentation/GenerateDocumentationFromPDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ namespace Microsoft.Contracts.Foxtrot
{
public class GenerateDocumentationFromPDB : Inspector
{
private int tabWidth;
readonly Func<string, string> sourceFileLocator;
readonly int tabWidth;
readonly bool writeOutput;
readonly ContractNodes contracts;
private int tabStops;
private bool writeOutput;
private ContractNodes contracts;

public GenerateDocumentationFromPDB(ContractNodes contracts) : this(contracts, 2, false)
public GenerateDocumentationFromPDB(ContractNodes contracts)
: this(contracts, null)
{
}

public GenerateDocumentationFromPDB(ContractNodes contracts, int tabWidth, bool writeOutput)
public GenerateDocumentationFromPDB(ContractNodes contracts, Func<string, string> sourceFileLocator)
: this(contracts, sourceFileLocator, 2, false)
{
}

public GenerateDocumentationFromPDB(ContractNodes contracts, Func<string, string> sourceFileLocator, int tabWidth, bool writeOutput)
{
this.contracts = contracts;
this.sourceFileLocator = sourceFileLocator;
this.tabWidth = tabWidth;
this.tabStops = 0;
this.writeOutput = writeOutput;
Expand Down Expand Up @@ -798,7 +806,13 @@ private SourceDocument GetDocument(Document doc)
SourceDocument result;
if (!sourceTexts.TryGetValue(sourceName, out result))
{
result = new SourceDocument(sourceName);
var sourceFilePath = sourceName;
if (sourceFileLocator != null)
{
sourceFilePath = sourceFileLocator(sourceFilePath);
}

result = new SourceDocument(sourceFilePath);
sourceTexts.Add(sourceName, result);
}

Expand Down
24 changes: 22 additions & 2 deletions Microsoft.Research/ClousotMain/ClousotMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ int InternalAnalyze()
foreach (string assembly in options.Assemblies)
{
Assembly assem;
if (!this.driver.MetaDataDecoder.TryLoadAssembly(assembly, assemblyCache, this.output.EmitError, out assem, options.IsLegacyAssemblyMode, options.reference, options.extractSourceText))
if (!this.driver.MetaDataDecoder.TryLoadAssembly(assembly, assemblyCache, this.output.EmitError, out assem, options.IsLegacyAssemblyMode, options.reference, options.extractSourceText, GetSourceFileLocator()))
{
output.WriteLine("Cannot load assembly '{0}'", assembly);
this.options.AddError();
Expand Down Expand Up @@ -1345,7 +1345,7 @@ private void PopulateContractAssemblies(IOutputResults output)
foreach (string contractAssembly in options.ContractAssemblies)
{
Assembly assem;
if (!driver.MetaDataDecoder.TryLoadAssembly(contractAssembly, assemblyCache, null, out assem, this.options.IsLegacyAssemblyMode, this.options.reference, this.options.extractSourceText))
if (!driver.MetaDataDecoder.TryLoadAssembly(contractAssembly, assemblyCache, null, out assem, this.options.IsLegacyAssemblyMode, this.options.reference, this.options.extractSourceText, null))
{
output.WriteLine("Cannot load contract assembly '{0}'", contractAssembly);
options.AddError();
Expand Down Expand Up @@ -3441,6 +3441,26 @@ public Unit Accept<Label, Handler>(IMethodCodeProvider<Label, Local, Parameter,
return Unit.Value;
}
}

private SourceFileLocator GetSourceFileLocator()
{
if (!options.extractSourceText)
{
return null;
}

if (options.sourcePaths == null || options.sourcePaths.Count == 0)
{
return null;
}

if (options.alternativeSourcePaths == null || options.alternativeSourcePaths.Count == 0)
{
return null;
}

return new SourceFileFinder(options.sourcePaths, options.alternativeSourcePaths).Find;
}
}

struct MethodAnalysisFlags
Expand Down
1 change: 1 addition & 0 deletions Microsoft.Research/ClousotMain/ClousotMain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<Compile Include="PriortizerOutput.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegressionOutput.cs" />
<Compile Include="SourceFileFinder.cs" />
<Compile Include="SwallowedCounterOutput.cs" />
<Compile Include="WarningSuggestionLinkOutput.cs" />
<Compile Include="XmlBaseLine.cs" />
Expand Down
8 changes: 8 additions & 0 deletions Microsoft.Research/ClousotMain/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ public void PrintUsage(TextWriter output)
[OptionDescription("Extract the source text for the contracts")]
public bool extractSourceText = true;

[OptionDescription("Paths to source files")]
[DoNotHashInCache]
public List<string> sourcePaths = new List<string>();

[OptionDescription("Alternative paths to search for source files")]
[DoNotHashInCache]
public List<string> alternativeSourcePaths = new List<string>();

[OptionDescription("Redirect the output to this output file")]
[DoNotHashInCache]
public string outFile;
Expand Down
81 changes: 81 additions & 0 deletions Microsoft.Research/ClousotMain/SourceFileFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// CodeContracts
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;

namespace Microsoft.Research.CodeAnalysis
{
[ContractVerification(true)]
class SourceFileFinder
{
public SourceFileFinder(IEnumerable<string> originalSourcePaths, IEnumerable<string> alternativeSourcePaths)
{
Contract.Requires(originalSourcePaths != null);
Contract.Requires(alternativeSourcePaths != null);

this.originalSourcePaths = originalSourcePaths;
this.alternativeSourcePaths = alternativeSourcePaths;
}

readonly IEnumerable<string> originalSourcePaths;
readonly IEnumerable<string> alternativeSourcePaths;
const StringComparison Comparison = StringComparison.OrdinalIgnoreCase; // Ignore case in Windows. Re-evalute this for Mono/.NET Core

public string Find(string originalPath)
{
if (originalPath == null || File.Exists(originalPath))
{
return originalPath;
}

foreach (var originalSourcePath in originalSourcePaths)
{
// Find the source path that this is a sub-path of.
if (!originalPath.StartsWith(originalSourcePath, Comparison))
{
continue;
}

// Extract the path relative to the source path;
var relativePath = originalPath.Substring(originalSourcePath.Length);

foreach (var alternativeSourcePath in alternativeSourcePaths)
{
// Graft the relative path onto the alternative source path, as though the file
// exists within that source tree instead.
var alternativePath = alternativeSourcePath + relativePath;

if (File.Exists(alternativePath))
{
return alternativePath;
}
}
}

return originalPath;
}

[ContractInvariantMethod]
void ObjectInvariant()
{
Contract.Invariant(originalSourcePaths != null);
Contract.Invariant(alternativeSourcePaths != null);
}
}
}
13 changes: 11 additions & 2 deletions Microsoft.Research/CodeProviders/CCI1/CodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ public Guid AssemblyGuid(AssemblyNode assem)
{
return assem.Mvid;
}
public bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out AssemblyNode assem, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText)
public bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out AssemblyNode assem, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText, SourceFileLocator sourceFileLocator)
{
var attacher = this.contractAttacher;
if (attacher == null)
Expand Down Expand Up @@ -2223,7 +2223,7 @@ public bool TryLoadAssembly(string fileName, System.Collections.IDictionary asse
if (extractContractText)
{
// extract doc first, then run checker
var gd = new Microsoft.Contracts.Foxtrot.GenerateDocumentationFromPDB(usedContracts);
var gd = new Microsoft.Contracts.Foxtrot.GenerateDocumentationFromPDB(usedContracts, AdaptSourceFileLocator(sourceFileLocator));
gd.VisitForDoc(assem);
}

Expand Down Expand Up @@ -2260,6 +2260,15 @@ public IEnumerable<AssemblyNode> AssemblyReferences(AssemblyNode assembly)
}
}

private static Func<string, string> AdaptSourceFileLocator(SourceFileLocator locator)
{
if (locator == null)
{
return null;
}
return new Func<string, string>(locator);
}

#endregion

#region Attributes
Expand Down
6 changes: 3 additions & 3 deletions Microsoft.Research/CodeProviders/CCI2/CodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9916,7 +9916,7 @@ public void RegisterSourceLocationProvider(IUnitReference unit, ISourceLocationP
/// <param name="unit">The unit the locations are from. (Locations don't necessarily have a link to the unit they
/// belong to.) The unit is used to find a source location provider for that unit. (Providers are maintained
/// in a cache in the MetadataDecoder.) If a provider cannot be found for the <paramref name="unit"/> then
/// an emtpy enumerable is returned.
/// an empty enumerable is returned.
/// </param>
/// <param name="operations">
/// If a source location provider is found, then the location of the operation indexed in <paramref name="operations"/>
Expand All @@ -9929,7 +9929,7 @@ public void RegisterSourceLocationProvider(IUnitReference unit, ISourceLocationP
/// The index of the operation from whose location the corresponding primary source locations are to be found.
/// </param>
/// <param name="exact">
/// True iff the client wants primary source locations only for the operation indexed by <paramref name="startingIndex"/>.
/// True if the client wants primary source locations only for the operation indexed by <paramref name="startingIndex"/>.
/// </param>
/// <param name="foundIndex">
/// If <paramref name="exact"/> is false, then this will be the greatest index of the operation in <paramref name="operations"/>
Expand Down Expand Up @@ -9984,7 +9984,7 @@ public void ProvideResidualMethodBody(IMethodDefinition methodDefinition, IBlock
/// <summary>
/// </summary>
/// <returns>Must return null if assembly cannot be loaded!</returns>
public bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out IAssemblyReference assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText)
public bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out IAssemblyReference assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText, SourceFileLocator sourceFileLocator)
{
IUnit unit = host.PossiblyCompileFirstLoadUnitFrom(fileName, referencedAssemblies, this.RegisterSourceLocationProvider);
if (unit is Dummy) { assembly = null; return false; }
Expand Down
9 changes: 7 additions & 2 deletions Microsoft.Research/ControlFlow/MetadataInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace Microsoft.Research.CodeAnalysis
/// </summary>
public delegate void BlockInfoPrinter<Label>(Label at, string prefix, TextWriter writer);

/// <summary>
/// Used to find the current location of a source file's path, e.g. from PDB
/// </summary>
public delegate string SourceFileLocator(string originalSourceFilePath);

#endregion

#region IL visitor data types
Expand Down Expand Up @@ -1647,7 +1652,7 @@ public partial interface IDecodeMetaData<Local, Parameter, Method, Field, Proper
/// </summary>
/// <returns>false if assembly cannot be loaded.</returns>
[Pure]
bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out Assembly assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractSourceText);
bool TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out Assembly assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractSourceText, SourceFileLocator sourceFileLocator);

/// <summary>
/// Returns all (and only) the top-level types in the given assembly.
Expand Down Expand Up @@ -3749,7 +3754,7 @@ Type IDecodeMetaData<Local, Parameter, Method, Field, Property, Event, Type, Att
throw new NotImplementedException();
}

bool IDecodeMetaData<Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>.TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out Assembly assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText)
bool IDecodeMetaData<Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>.TryLoadAssembly(string fileName, System.Collections.IDictionary assemblyCache, Action<System.CodeDom.Compiler.CompilerError> errorHandler, out Assembly assembly, bool legacyContractMode, List<string> referencedAssemblies, bool extractContractText, SourceFileLocator sourceFileLocator)
{
throw new NotImplementedException();
}
Expand Down