Skip to content

Commit

Permalink
Improvements to the compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Oct 19, 2023
1 parent ff23b12 commit faf5507
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 51 deletions.
54 changes: 20 additions & 34 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,19 @@ func (v *Compiler) Compile() (*CompilerResults, error) {
zap.L().Error(
"Failed to compile Solidity sources",
zap.String("version", compilerVersion),
zap.String("stdout", out.String()),
zap.String("stderr", stderr.String()),
)
var errors []CompilationError
var warnings []CompilationError

// Parsing the error message to extract line and column information.
errorMessage := stderr.String()
if strings.Contains(errorMessage, "Error:") {
errors = append(errors, CompilationError{Message: errorMessage})
} else if strings.HasPrefix(errorMessage, "Warning:") {
warnings = append(warnings, CompilationError{Message: errorMessage})
}
errors = append(errors, CompilationError{Message: errorMessage})

// Construct the CompilerResults structure with errors and warnings.
results := &CompilerResult{
RequestedVersion: compilerVersion,
Errors: errors,
Warnings: warnings,
}
return &CompilerResults{Results: []*CompilerResult{results}}, err
}
Expand Down Expand Up @@ -159,13 +154,9 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
}

// Separate errors and warnings
var errors, warnings []CompilationError
var errors []CompilationError
for _, msg := range compilationOutput.Errors {
if strings.Contains(msg, "Warning:") {
warnings = append(warnings, CompilationError{Message: msg})
} else {
errors = append(errors, CompilationError{Message: msg})
}
errors = append(errors, CompilationError{Message: msg})
}

var results []*CompilerResult
Expand All @@ -189,7 +180,6 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
ABI: string(abi),
ContractName: strings.TrimLeft(key, "<stdin>:"),
Errors: errors,
Warnings: warnings,
})
}

Expand Down Expand Up @@ -268,13 +258,20 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) (*C
return &CompilerResults{Results: results}, nil
}

type CompilationErrorSourceLocation struct {
File string `json:"file"`
Start int `json:"start"`
End int `json:"end"`
}

// CompilationError represents a compilation error.
type CompilationError struct {
Component string `json:"component"`
Formatted string `json:"formattedMessage"`
Message string `json:"message"`
Severity string `json:"severity"`
Type string `json:"type"`
Component string `json:"component"`
Formatted string `json:"formatted_message"`
Message string `json:"message"`
Severity string `json:"severity"`
Type string `json:"type"`
SourceLocation CompilationErrorSourceLocation `json:"sourceLocation"`
}

type CompilerResults struct {
Expand All @@ -286,6 +283,10 @@ func (cr *CompilerResults) GetResults() []*CompilerResult {
}

func (cr *CompilerResults) GetEntryContract() *CompilerResult {
if cr == nil {
return nil
}

for _, result := range cr.Results {
if result.IsEntry() {
return result
Expand All @@ -307,7 +308,6 @@ type CompilerResult struct {
Opcodes string `json:"opcodes"`
Metadata string `json:"metadata"`
Errors []CompilationError `json:"errors"`
Warnings []CompilationError `json:"warnings"`
}

// IsEntry returns true if the compiled contract is the entry contract.
Expand All @@ -334,25 +334,11 @@ func (v *CompilerResult) HasErrors() bool {
return len(v.Errors) > 0
}

// HasWarnings returns true if there are compilation warnings.
func (v *CompilerResult) HasWarnings() bool {
if v == nil {
return false
}

return len(v.Warnings) > 0
}

// GetErrors returns the compilation errors.
func (v *CompilerResult) GetErrors() []CompilationError {
return v.Errors
}

// GetWarnings returns the compilation warnings.
func (v *CompilerResult) GetWarnings() []CompilationError {
return v.Warnings
}

// GetABI returns the compiled contract's ABI (Application Binary Interface) in JSON format.
func (v *CompilerResult) GetABI() string {
return v.ABI
Expand Down
47 changes: 39 additions & 8 deletions compiler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,45 @@ import (

// allowedArgs defines a list of allowed arguments for solc.
var allowedArgs = map[string]bool{
"--combined-json": true,
"-": true,
"--optimize": true,
"--optimize-runs": true,
"--evm-version": true,
"--overwrite": true,
"--libraries": true,
"--standard-json": true,
"--combined-json": true,
"-": true,
"--optimize": true,
"--optimize-runs": true,
"--evm-version": true,
"--overwrite": true,
"--libraries": true,
"--standard-json": true,
"--allow-paths": true,
"--base-path": true,
"--ignore-missing": true,
"--ast": true,
"--ast-json": true,
"--include-path": true,
"--output-dir": true,
"--asm": true,
"--bin": true,
"--abi": true,
"--asm-json": true,
"--bin-runtime": true,
"--ir": true,
"--opcodes": true,
"--ir-optimized": true,
"--ewasm": true,
"--ewasm-ir": true,
"--no-optimize-yul": true,
"--yul-optimizations": true,
"--yul": true,
"--assemble": true,
"--lsp": true,
"--hashes": true,
"--userdoc": true,
"--devdoc": true,
"--metadata": true,
"--storage-layout": true,
"--gas": true,
"--metadata-hash": true,
"--metadata-literal": true,
"--error-recovery": true,
}

// requiredArgs defines a list of required arguments for solc.
Expand Down
7 changes: 0 additions & 7 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ func TestCompiler(t *testing.T) {
if testCase.wantCompileErr {
for _, result := range compilerResults.GetResults() {
assert.True(t, result.HasErrors())
assert.False(t, result.HasWarnings())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 1)
}

Expand All @@ -231,7 +229,6 @@ func TestCompiler(t *testing.T) {
assert.NotEmpty(t, result.GetBytecode())
assert.NotEmpty(t, result.GetABI())
assert.NotEmpty(t, result.GetContractName())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 0)
}
})
Expand Down Expand Up @@ -428,7 +425,6 @@ func TestCompilerFromSolc(t *testing.T) {
assert.NotEmpty(t, result.GetBytecode())
assert.NotEmpty(t, result.GetABI())
assert.NotEmpty(t, result.GetContractName())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 0)
}
})
Expand Down Expand Up @@ -547,8 +543,6 @@ func TestCompilerWithJSON(t *testing.T) {
if testCase.wantCompileErr {
for _, result := range compilerResults.GetResults() {
assert.True(t, result.HasErrors())
assert.False(t, result.HasWarnings())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 1)
}

Expand All @@ -568,7 +562,6 @@ func TestCompilerWithJSON(t *testing.T) {
assert.NotEmpty(t, result.GetContractName())
assert.NotEmpty(t, result.GetOpcodes())
assert.NotEmpty(t, result.GetMetadata())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 0)
}
})
Expand Down
4 changes: 2 additions & 2 deletions releases/releases.json
Git LFS file not shown

0 comments on commit faf5507

Please sign in to comment.