Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C support #663

Open
wants to merge 4 commits into
base: main
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
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
semantic/dist-newstyle
semantic-analysis/dist-newstyle
semantic-ast/dist-newstyle
semantic-c/dist-newstyle
semantic-codeql/dist-newstyle
semantic-core/dist-newstyle
semantic-go/dist-newstyle
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
cabal v2-build --project-file=cabal.project.ci semantic:exe:semantic
cabal v2-run --project-file=cabal.project.ci semantic:test
cabal v2-run --project-file=cabal.project.ci semantic-tags:test
cabal v2-run --project-file=cabal.project.ci semantic-c:test
cabal v2-run --project-file=cabal.project.ci semantic-codeql:test
cabal v2-run --project-file=cabal.project.ci semantic-go:test
cabal v2-run --project-file=cabal.project.ci semantic-java:test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Available options:

| Language | Parse | AST Symbols† | Stack graphs |
| :------------- | :---: | :---: | :---: |
| C || ⬜️ | |
| Ruby ||| |
| JavaScript ||| |
| TypeScript ||| 🚧 |
Expand Down
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
packages: semantic
semantic-analysis
semantic-ast
semantic-c
semantic-codeql
semantic-core
semantic-go
Expand Down
4 changes: 4 additions & 0 deletions cabal.project.ci
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
packages: semantic
semantic-analysis
semantic-ast
semantic-c
semantic-codeql
semantic-core
semantic-go
Expand Down Expand Up @@ -31,6 +32,9 @@ package semantic-analysis
package semantic-ast
ghc-options: -Werror

package semantic-c
ghc-options: -Werror

package semantic-codeql
ghc-options: -Werror

Expand Down
2 changes: 2 additions & 0 deletions script/ghci-flags
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function flags {
echo "-isemantic/test"
echo "-isemantic-analysis/src"
echo "-isemantic-ast/src"
echo "-isemantic-c/src"
echo "-isemantic-c/test"
echo "-isemantic-codeql/src"
echo "-isemantic-codeql/test"
echo "-isemantic-core/src"
Expand Down
1 change: 1 addition & 0 deletions script/ghci-flags-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ echo "semantic-analysis/semantic-analysis.cabal"
echo "semantic-ast/semantic-ast.cabal"
echo "semantic-core/semantic-core.cabal"
echo "semantic-tags/semantic-tags.cabal"
echo "semantic-c/semantic-c.cabal"
echo "semantic-go/semantic-go.cabal"
echo "semantic-java/semantic-java.cabal"
echo "semantic-json/semantic-json.cabal"
Expand Down
2 changes: 2 additions & 0 deletions semantic-ast/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ haskell_library(
)

all_ts_deps = ["@tree-sitter-{name}".format(name = name) for name in [
"c",
"go",
"java",
"json",
Expand All @@ -62,6 +63,7 @@ all_ts_deps = ["@tree-sitter-{name}".format(name = name) for name in [
]]

all_file_deps = ["@tree-sitter-{name}//:src/node-types.json".format(name = name) for name in [
"c",
"go",
"java",
"json",
Expand Down
4 changes: 2 additions & 2 deletions semantic-ast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ This package is concerned with the CodeGen generation of strongly-typed ASTs.
The provided `semantic-ast` executable is responsible for generating ASTs from language definitions. You can run it like so:

```
cabal run semantic-ast -- --language=JSON
RUNFILES_DIR=path/to/haskell-tree-sitter cabal run semantic-ast -- --rootdir=. --language=JSON
```

You can also pass `all` to regenerate every language definition:

```
cabal run semantic-ast -- --language=all
RUNFILES_DIR=path/to/haskell-tree-sitter cabal run semantic-ast -- --rootdir=. --language=all
```

[Documentation](https://github.com/github/semantic/blob/master/docs/codegen.md)
6 changes: 5 additions & 1 deletion semantic-ast/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import System.Exit
import System.IO
import System.Process
import Text.Printf
import qualified TreeSitter.C as C (tree_sitter_c)
import qualified TreeSitter.Go as Go (tree_sitter_go)
import qualified TreeSitter.JSON as JSON (tree_sitter_json)
import qualified TreeSitter.Java as Java (tree_sitter_java)
Expand Down Expand Up @@ -72,6 +73,7 @@ pathForLanguage :: Bazel.Runfiles -> Language -> FilePath
pathForLanguage rf =
let loc = Bazel.rlocation rf
in \case
C -> loc "tree-sitter-c/vendor/tree-sitter-c/src/node-types.json"
CodeQL -> loc "tree-sitter-ql/vendor/tree-sitter-ql/src/node-types.json"
Go -> loc "tree-sitter-go/vendor/tree-sitter-go/src/node-types.json"
PHP -> loc "tree-sitter-php/vendor/tree-sitter-php/src/node-types.json"
Expand All @@ -88,6 +90,7 @@ targetForLanguage :: Language -> FilePath
targetForLanguage x =
let go lc = printf "semantic-%s/src/Language/%s/AST.hs" (lc :: String) (show x)
in case x of
C -> go "c"
CodeQL -> go "codeql"
Go -> go "go"
PHP -> go "php"
Expand All @@ -102,6 +105,7 @@ targetForLanguage x =
parserForLanguage :: Language -> Ptr TreeSitter.Language.Language
parserForLanguage = \case
Unknown -> error "Unknown language encountered"
C -> C.tree_sitter_c
CodeQL -> (CodeQL.tree_sitter_ql)
Go -> Go.tree_sitter_go
Haskell -> error "Haskell backend not implemented yet"
Expand All @@ -121,7 +125,7 @@ parserForLanguage = \case
-- CodeQL -> r

validLanguages :: [Language]
validLanguages = [CodeQL, Go, Java, PHP, Python, Ruby, TypeScript, TSX]
validLanguages = [C, CodeQL, Go, Java, PHP, Python, Ruby, TypeScript, TSX]

emit :: FilePath -> Language -> IO ()
emit root lang = do
Expand Down
31 changes: 31 additions & 0 deletions semantic-ast/semantic-ast.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,34 @@ library
, unordered-containers ^>= 0.2.10

hs-source-dirs: src

executable generate-ast
import: haskell
hs-source-dirs: app
main-is: Main.hs
build-depends:
semantic-ast,
base,
filepath,
process,
template-haskell,
text,
semantic-source,
bazel-runfiles,
directory,
generic-lens,
lens,
neat-interpolation,
optparse-generic,
tree-sitter,
tree-sitter-c,
tree-sitter-go,
tree-sitter-java,
tree-sitter-json,
tree-sitter-php,
tree-sitter-python,
tree-sitter-ql,
tree-sitter-ruby,
tree-sitter-rust,
tree-sitter-tsx,
tree-sitter-typescript
8 changes: 7 additions & 1 deletion semantic-ast/src/AST/GenerateSyntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ toName :: Named -> String -> Name
toName named str = mkName (toNameString named str)

toNameString :: Named -> String -> String
toNameString named str = prefix named <> toHaskellPascalCaseIdentifier str
toNameString named str = prefix named <> toHaskellPascalCaseIdentifier (dedup str)
where
-- C’s u' and U' conflict
-- TODO: resolve this in tree-sitter-c
dedup "u'" = "u16'"
dedup "u\"" = "u16\""
dedup s = s

prefix Anonymous = "Anonymous"
prefix Named = ""

Expand Down
19 changes: 19 additions & 0 deletions semantic-c/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])

load(
"//:build/common.bzl",
"semantic_language_library",
"semantic_language_parsing_test",
)

semantic_language_library(
name = "semantic-c",
srcs = glob(["src/**/*.hs"]),
language = "c",
)

semantic_language_parsing_test(
language = "c",
semantic_package = "c",
ts_package = "c",
)
21 changes: 21 additions & 0 deletions semantic-c/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 GitHub

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.
3 changes: 3 additions & 0 deletions semantic-c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Semantic support for C

This package implements `semantic` support for C using the `semantic-core` intermediate language.
2 changes: 2 additions & 0 deletions semantic-c/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
74 changes: 74 additions & 0 deletions semantic-c/semantic-c.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
cabal-version: 2.4

name: semantic-c
version: 0.0.0.0
synopsis: Semantic support for C.
description: Semantic support for C using the semantic-core intermediate language.
homepage: https://github.com/github/semantic/tree/master/semantic-c#readme
bug-reports: https://github.com/github/semantic/issues
license: MIT
license-file: LICENSE
author: The Semantic authors
maintainer: [email protected]
copyright: (c) 2019 GitHub, Inc.
category: Language
build-type: Simple
stability: alpha
extra-source-files: README.md

tested-with: GHC == 8.6.5

common haskell
default-language: Haskell2010
ghc-options:
-Weverything
-Wno-missing-local-signatures
-Wno-missing-import-lists
-Wno-implicit-prelude
-Wno-safe
-Wno-unsafe
-Wno-name-shadowing
-Wno-monomorphism-restriction
-Wno-missed-specialisations
-Wno-all-missed-specialisations
-Wno-star-is-type
if (impl(ghc >= 8.8))
ghc-options: -Wno-missing-deriving-strategies
if (impl(ghc >= 8.10))
ghc-options:
-Wno-missing-safe-haskell-mode
-Wno-prepositive-qualified-module
cpp-options:
-DBAZEL_BUILD=0

library
import: haskell
exposed-modules:
Language.C
Language.C.AST
Language.C.Grammar
Language.C.Tags
hs-source-dirs: src
build-depends:
, base >= 4.13 && < 5
, fused-effects ^>= 1.1
, semantic-ast
, semantic-source ^>= 0.1.0.1
, semantic-tags ^>= 0.0
, template-haskell >= 2.15 && < 2.17
, text ^>= 1.2.3
, tree-sitter ^>= 0.9
, tree-sitter-c ^>= 0.1.0.0

test-suite test
import: haskell
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: PreciseTest.hs
build-depends:
, base
, pathtype ^>= 0.8.1
, semantic-ast
, semantic-c
, tasty
, tree-sitter-c ^>= 0.1.0.0
28 changes: 28 additions & 0 deletions semantic-c/src/Language/C.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -freduction-depth=0 #-}
-- | Semantic functionality for C programs.
module Language.C
( Term(..)
, TreeSitter.C.tree_sitter_c
) where

import AST.Marshal.JSON
import qualified AST.Unmarshal as TS
import Data.Proxy
import qualified Language.C.AST as C
import qualified Language.C.Tags as CTags
import qualified Tags.Tagging.Precise as Tags
import qualified TreeSitter.C (tree_sitter_c)

newtype Term a = Term { getTerm :: C.TranslationUnit a }
deriving MarshalJSON

instance TS.SymbolMatching Term where
matchedSymbols _ = TS.matchedSymbols (Proxy :: Proxy C.TranslationUnit)
showFailure _ = TS.showFailure (Proxy :: Proxy C.TranslationUnit)

instance TS.Unmarshal Term where
matchers = fmap (fmap (TS.hoist Term)) TS.matchers

instance Tags.ToTags Term where
tags src = Tags.runTagging src . CTags.tags . getTerm
Loading