Skip to content
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
15 changes: 12 additions & 3 deletions clang/compilerDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package clang

import (
"encoding/json"
"errors"
"github.com/ejfitzgerald/clang-tidy-cache/utils"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/ejfitzgerald/clang-tidy-cache/utils"
)

type DatabaseEntry struct {
Expand All @@ -32,18 +33,26 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database
defer jsonFile.Close()

bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}

var db Database
err = json.Unmarshal(bytes, &db)
if err != nil {
return nil, err
}

// Find the entry that matches the target
for _, entry := range db {
entry.File = utils.NormalizePath(entry.File)
entry.Directory = utils.NormalizePath(entry.Directory)
target = utils.NormalizePath(target)

if entry.File == target || entry.File == filepath.Join(entry.Directory, target) {
return &entry, nil
}
}

return nil, errors.New("Unable to locate the compiler definition")
return nil, fmt.Errorf("unable to find the compiler definition for target %v", target)
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package main
import (
"encoding/json"
"fmt"
"github.com/ejfitzgerald/clang-tidy-cache/caches"
"github.com/ejfitzgerald/clang-tidy-cache/clang"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"

"github.com/ejfitzgerald/clang-tidy-cache/caches"
"github.com/ejfitzgerald/clang-tidy-cache/clang"
)

const VERSION = "0.3.0"
Expand Down
30 changes: 30 additions & 0 deletions utils/fileSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

// FindInParents searches for a file named filename in searchDir or any of it's
Expand All @@ -22,3 +23,32 @@ func FindInParents(searchDir string, filename string) (string, error) {
}
return FindInParents(parentDir, filename)
}

// Converts given path to Posix (replacing \ with /)
//
// @param {string} givenPath Path to convert
//
// @returns {string} Converted filepath
func PosixifyPath(givenPath string) string {
return strings.ReplaceAll(givenPath, "\\", "/")
}

// NormalizePath normalizes the given path between Windows and POSIX
// and removes the leading "./" if present
//
// @param {string} path Path to normalize
//
// @returns {string} Normalized path
func NormalizePath(path string) string {
var normalizedPath string

// Normalize the path to use POSIX separators
normalizedPath = PosixifyPath(path)

// Remove the leading "./" if present
if normalizedPath[0:2] == "./" {
normalizedPath = normalizedPath[2:]
}

return normalizedPath
}