Skip to content

Commit

Permalink
feat: Add support for passing the path to a C compiler to check-c.
Browse files Browse the repository at this point in the history
Currently it hard-codes "clang". This makes it overrideable.
  • Loading branch information
iphydf committed Dec 15, 2023
1 parent 7632281 commit 165754a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ hspec_test(
"//third_party/haskell:text",
],
)

filegroup(
name = "headers",
srcs = glob(["include/**/*.h"]),
visibility = ["//visibility:public"],
)
17 changes: 11 additions & 6 deletions tools/check-c.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module Main (main) where

import Control.Monad (forM_, unless, zipWithM_)
import qualified Control.Monad.Parallel as Par
import Data.List (isPrefixOf, partition)
import Data.List (find, isPrefixOf, partition)
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import Language.C
import Language.C.Analysis.AstAnalysis
import Language.C.Analysis.ConstEval
Expand Down Expand Up @@ -409,9 +410,9 @@ defaultCppOpts sysInclude =
, "-I" <> sysInclude <> "/opus"
]

processFile :: String -> CLanguage -> [String] -> FilePath -> IO (Bool, (String, [String]))
processFile sysInclude lang cppOpts file = do
result <- parseCFile (newGCC "clang") Nothing (defaultCppOpts sysInclude ++ cppOpts) file
processFile :: String -> String -> CLanguage -> [String] -> FilePath -> IO (Bool, (String, [String]))
processFile cc sysInclude lang cppOpts file = do
result <- parseCFile (newGCC cc) Nothing (defaultCppOpts sysInclude ++ cppOpts) file
case result of
Left err -> return (False, (file, ["Parse Error: " <> show err]))
Right tu ->
Expand All @@ -428,12 +429,16 @@ processFile sysInclude lang cppOpts file = do
main :: IO ()
main = do
args <- getArgs
let (cppOpts, files) = partition (isPrefixOf "-") args
let (opts, rest) = partition (isPrefixOf "--") args
let (cppOpts, files) = partition (isPrefixOf "-") rest
let cc = Maybe.fromMaybe "clang" $ getFlag "--cc=" opts
let sysInclude = "/src/workspace/hs-tokstyle/include"
result <- Par.mapM (processFile sysInclude GNU99 cppOpts) files
result <- Par.mapM (processFile cc sysInclude GNU99 cppOpts) files
mapM_ (printResult . snd) result
unless (all fst result) $ exitWith (ExitFailure 1)
where
printResult (file, result) = do
hPutStr stderr $ file <> ": "
mapM_ (hPutStrLn stderr) result

getFlag flag = fmap (drop $ length flag) . find (isPrefixOf flag)

0 comments on commit 165754a

Please # to comment.