Skip to content

Commit

Permalink
Add symbol id lookup for Pp symbols
Browse files Browse the repository at this point in the history
Summary:
Update symbol id encoding for pp.angle things to be more precise.
Then implement symbol lookup.

This is sufficient to give us describe() , symbol pages and hover text.

Reviewed By: mpark

Differential Revision: D48059438

fbshipit-source-id: e476eb6d3548d61e4e7c202cbcd29e4e72b3fb99
  • Loading branch information
donsbot authored and facebook-github-bot committed Aug 4, 2023
1 parent a045502 commit 60343a6
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 66 deletions.
4 changes: 3 additions & 1 deletion glean/glass/Glean/Glass/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import qualified Glean.Glass.Search.Haskell ({- instances -})
import qualified Glean.Glass.Search.Java ({- instances -})
import qualified Glean.Glass.Search.Kotlin ({- instances -})
import qualified Glean.Glass.Search.LSIF ({- instances -})
import qualified Glean.Glass.Search.SCIP ({- instances -})
import qualified Glean.Glass.Search.Pp ({- instances -})
import qualified Glean.Glass.Search.Python ({- instances -})
import qualified Glean.Glass.Search.SCIP ({- instances -})
import qualified Glean.Glass.Search.Thrift ({- instances -})
import Glean.Glass.Types (ServerException(ServerException))
import qualified Glean.Schema.Code.Types as Code
Expand Down Expand Up @@ -66,6 +67,7 @@ searchEntity
searchEntity lang toks = case lang of
Language_Cpp -> fmap Code.Entity_cxx <$> Search.symbolSearch toks
Language_Hack -> fmap Code.Entity_hack <$> Search.symbolSearch toks
Language_PreProcessor -> fmap Code.Entity_pp <$> Search.symbolSearch toks
Language_Python -> fmap Code.Entity_python <$> Search.symbolSearch toks
Language_JavaScript -> fmap Code.Entity_flow <$> Search.symbolSearch toks
Language_Haskell -> fmap Code.Entity_hs <$> Search.symbolSearch toks
Expand Down
105 changes: 105 additions & 0 deletions glean/glass/Glean/Glass/Search/Pp.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{-
Copyright (c) Meta Platforms, Inc. and affiliates.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
-}

{-# LANGUAGE TypeApplications, ApplicativeDo, PartialTypeSignatures #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Glean.Glass.Search.Pp
( {- instances -}
) where

import Data.Text ( Text )

import Glean.Glass.Search.Class
import Glean.Angle as Angle
import Glean.Glass.Query ( entityLocation )
import Glean.Glass.Utils ( joinFragments )

import qualified Glean.Schema.CodePp.Types as Pp
import qualified Glean.Schema.CodemarkupTypes.Types as Code
import qualified Glean.Schema.Pp1.Types as Pp
import qualified Glean.Schema.Src.Types as Src

breakPathAndName :: [Text] -> Maybe (Path, Name)
breakPathAndName [] = Nothing
breakPathAndName [_name] = Nothing -- has to be a file and a name
breakPathAndName toks@(_:_) = Just (Path path, Name name)
where
path = joinFragments (init toks)
name = last toks

data Query
= Define !Path !Name
| Undef !Path !Name
| Include !Path

newtype Path = Path Text
newtype Name = Name Text

instance Search Pp.Entity where
symbolSearch toks = case toks of
[] -> return $ None "PP.symbolSearch: empty"
[_] -> return $ None "PP.symbolSearch: singleton: not a symbolid"
base:rest -> case base of
"define" -> case breakPathAndName rest of
Nothing -> return $ None "Pp.symbolSearch: define: missing name"
Just (path, name) -> searchSymbolId toks $ runQuery (Define path name)
"undef" -> case breakPathAndName rest of
Nothing -> return $ None "Pp.symbolSearch: undef: missing name"
Just (path, name) -> searchSymbolId toks $ runQuery (Undef path name)
_ -> searchSymbolId toks $ runQuery (Include (Path (joinFragments toks)))

runQuery :: Query -> Angle (ResultLocation Pp.Entity)
runQuery (Define path name) = searchDefine path name
runQuery (Undef path name) = searchUndef path name
runQuery (Include path) = searchInclude path

searchDefine :: Path -> Name -> Angle (ResultLocation Pp.Entity)
searchDefine (Path path) (Name name) =
vars $ \(entity :: Angle Pp.Entity) (file :: Angle Src.File)
(decl :: Angle Pp.Define) (rangespan :: Angle Code.RangeSpan)
(lname :: Angle Text) ->
tuple (entity,file,rangespan,lname) `where_` [
file .= predicate @Src.File (string path),
decl .= predicate @Pp.Define (
rec $
field @"macro" (string name) $
field @"source" (rec $ field @"file" (asPredicate file) end)
end),
alt @"define" (asPredicate decl) .= sig entity,
entityLocation (alt @"pp" entity) file rangespan lname
]

searchUndef :: Path -> Name -> Angle (ResultLocation Pp.Entity)
searchUndef (Path path) (Name name) =
vars $ \(entity :: Angle Pp.Entity) (file :: Angle Src.File)
(decl :: Angle Pp.Undef) (rangespan :: Angle Code.RangeSpan)
(lname :: Angle Text) ->
tuple (entity,file,rangespan,lname) `where_` [
file .= predicate @Src.File (string path),
decl .= predicate @Pp.Undef (
rec $
field @"macro" (string name) $
field @"source" (rec $ field @"file" (asPredicate file) end)
end),
alt @"undef" (asPredicate decl) .= sig entity,
entityLocation (alt @"pp" entity) file rangespan lname
]

searchInclude :: Path -> Angle (ResultLocation Pp.Entity)
searchInclude (Path path) =
vars $ \(entity :: Angle Pp.Entity) (file :: Angle Src.File)
(rangespan :: Angle Code.RangeSpan) (lname :: Angle Text) ->
tuple (entity,file,rangespan,lname) `where_` [
file .= predicate @Src.File (string path),
wild .= predicate @Pp.Include ( -- asserts it is a member of pp.Include
rec $ field @"file" (asPredicate file) end),
alt @"include_" (asPredicate file) .= sig entity,
entityLocation (alt @"pp" entity) file rangespan lname
]
6 changes: 4 additions & 2 deletions glean/glass/Glean/Glass/SymbolId/Pp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ instance Symbol Pp.Entity where
Pp.Entity_EMPTY -> return []

instance Symbol Pp1.Define_key where
toSymbol (Pp1.Define_key macro source) = toSymbolMacro macro source
toSymbol (Pp1.Define_key macro source) =
(\x -> "define" : x) <$> toSymbolMacro macro source

instance Symbol Pp1.Undef_key where
toSymbol (Pp1.Undef_key macro source) = toSymbolMacro macro source
toSymbol (Pp1.Undef_key macro source) =
(\x -> "undef" : x) <$> toSymbolMacro macro source

instance Symbol Src.File where
toSymbol k = pathFragments <$> Glean.keyOf k
Expand Down
50 changes: 50 additions & 0 deletions glean/glass/test/regression/tests/cpp/describe_symbol_macro.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
"@generated",
{
"comments": [],
"contains_relation": {
"firstChild": "nondeterministic",
"firstParent": "nondeterministic",
"hasMoreChildren": false,
"hasMoreParents": false
},
"extends_relation": {
"firstChild": "nondeterministic",
"firstParent": "nondeterministic",
"hasMoreChildren": false,
"hasMoreParents": false
},
"kind": 30,
"language": 8,
"location": {
"filepath": "test2.h",
"range": {
"columnBegin": 9,
"columnEnd": 12,
"lineBegin": 10,
"lineEnd": 10
},
"repository": "test"
},
"modifiers": [],
"name": {
"container": "test2.h",
"localName": "FOO"
},
"pretty_comments": [],
"repo_hash": "testhash",
"sym": "test/pp/define/test2.h/FOO",
"sym_location": {
"filepath": "test2.h",
"range": {
"columnBegin": 9,
"columnEnd": 12,
"lineBegin": 10,
"lineEnd": 10
},
"repository": "test"
},
"sym_other_locations": [],
"type_xrefs": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
action: describeSymbol
args: "test/pp/define/test2.h/FOO"
50 changes: 50 additions & 0 deletions glean/glass/test/regression/tests/cpp/describe_symbol_macro_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
"@generated",
{
"comments": [],
"contains_relation": {
"firstChild": "nondeterministic",
"firstParent": "nondeterministic",
"hasMoreChildren": false,
"hasMoreParents": false
},
"extends_relation": {
"firstChild": "nondeterministic",
"firstParent": "nondeterministic",
"hasMoreChildren": false,
"hasMoreParents": false
},
"kind": 30,
"language": 8,
"location": {
"filepath": "test.cpp",
"range": {
"columnBegin": 9,
"columnEnd": 10,
"lineBegin": 87,
"lineEnd": 87
},
"repository": "test"
},
"modifiers": [],
"name": {
"container": "test.cpp",
"localName": "A"
},
"pretty_comments": [],
"repo_hash": "testhash",
"sym": "test/pp/define/test.cpp/A",
"sym_location": {
"filepath": "test.cpp",
"range": {
"columnBegin": 9,
"columnEnd": 10,
"lineBegin": 87,
"lineEnd": 87
},
"repository": "test"
},
"sym_other_locations": [],
"type_xrefs": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
action: describeSymbol
args: "test/pp/define/test.cpp/A"
6 changes: 3 additions & 3 deletions glean/glass/test/regression/tests/cpp/documentSymbolIndex.out
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@
"lineBegin": 25,
"lineEnd": 25
},
"sym": "test/pp/test2.h/FOO",
"sym": "test/pp/define/test2.h/FOO",
"target": {
"filepath": "test2.h",
"range": {
Expand Down Expand Up @@ -4598,7 +4598,7 @@
"lineBegin": 87,
"lineEnd": 87
},
"sym": "test/pp/test.cpp/A"
"sym": "test/pp/define/test.cpp/A"
}
],
"88": [
Expand Down Expand Up @@ -4626,7 +4626,7 @@
"lineBegin": 88,
"lineEnd": 88
},
"sym": "test/pp/test.cpp/B"
"sym": "test/pp/define/test.cpp/B"
}
],
"9": [
Expand Down
Loading

0 comments on commit 60343a6

Please sign in to comment.