-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate necessary config files for the Clang tool on the Go side.
PiperOrigin-RevId: 655682520
- Loading branch information
1 parent
a5d0c68
commit 842894d
Showing
3 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 The gVisor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// ClangASTConfig is the format for compilation_commands.json. | ||
type ClangASTConfig struct { | ||
Directory string `json:"directory"` | ||
Arguments []string `json:"arguments"` | ||
Filename string `json:"file"` | ||
} | ||
|
||
// NewParserConfig creates a ClangASTConfig for the given file using the list of includes. | ||
func NewParserConfig(directory, filename string, includes []string) ClangASTConfig { | ||
args := []string{"clang"} | ||
for _, include := range includes { | ||
args = append(args, "-I", include) | ||
} | ||
args = append(args, filename) | ||
|
||
return ClangASTConfig{ | ||
Directory: directory, | ||
Arguments: args, | ||
Filename: filename, | ||
} | ||
} | ||
|
||
// CreateCompileCommandsFile writes the given config to file. | ||
func CreateCompileCommandsFile(config []ClangASTConfig, path string) error { | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to create %s: %w", path, err) | ||
} | ||
defer f.Close() | ||
|
||
if err := json.NewEncoder(f).Encode(config); err != nil { | ||
return fmt.Errorf("failed to write config to file: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters