@@ -14,7 +14,7 @@ namespace CppAst
14
14
{
15
15
static internal unsafe class CppTokenUtil
16
16
{
17
- public static void ParseCursorAttributs ( CXCursor cursor , ref List < CppAttribute > attributes )
17
+ public static void ParseCursorAttributs ( CppGlobalDeclarationContainer globalContainer , CXCursor cursor , ref List < CppAttribute > attributes )
18
18
{
19
19
var tokenizer = new AttributeTokenizer ( cursor ) ;
20
20
var tokenIt = new TokenIterator ( tokenizer ) ;
@@ -25,7 +25,7 @@ public static void ParseCursorAttributs(CXCursor cursor, ref List<CppAttribute>
25
25
26
26
while ( tokenIt . CanPeek )
27
27
{
28
- if ( ParseAttributes ( tokenIt , ref attributes ) )
28
+ if ( ParseAttributes ( globalContainer , tokenIt , ref attributes ) )
29
29
{
30
30
continue ;
31
31
}
@@ -42,7 +42,7 @@ public static void ParseCursorAttributs(CXCursor cursor, ref List<CppAttribute>
42
42
}
43
43
44
44
45
- public static void ParseFunctionAttributes ( CXCursor cursor , string functionName , ref List < CppAttribute > attributes )
45
+ public static void ParseFunctionAttributes ( CppGlobalDeclarationContainer globalContainer , CXCursor cursor , string functionName , ref List < CppAttribute > attributes )
46
46
{
47
47
// TODO: This function is not 100% correct when parsing tokens up to the function name
48
48
// we assume to find the function name immediately followed by a `(`
@@ -58,7 +58,7 @@ public static void ParseFunctionAttributes(CXCursor cursor, string functionName,
58
58
// Parse leading attributes
59
59
while ( tokenIt . CanPeek )
60
60
{
61
- if ( ParseAttributes ( tokenIt , ref attributes ) )
61
+ if ( ParseAttributes ( globalContainer , tokenIt , ref attributes ) )
62
62
{
63
63
continue ;
64
64
}
@@ -103,7 +103,7 @@ public static void ParseFunctionAttributes(CXCursor cursor, string functionName,
103
103
104
104
while ( tokenIt . CanPeek )
105
105
{
106
- if ( ParseAttributes ( tokenIt , ref attributes ) )
106
+ if ( ParseAttributes ( globalContainer , tokenIt , ref attributes ) )
107
107
{
108
108
continue ;
109
109
}
@@ -115,7 +115,7 @@ public static void ParseFunctionAttributes(CXCursor cursor, string functionName,
115
115
}
116
116
117
117
118
- public static void ParseAttributesInRange ( CXTranslationUnit tu , CXSourceRange range , ref List < CppAttribute > collectAttributes )
118
+ public static void ParseAttributesInRange ( CppGlobalDeclarationContainer globalContainer , CXTranslationUnit tu , CXSourceRange range , ref List < CppAttribute > collectAttributes )
119
119
{
120
120
var tokenizer = new AttributeTokenizer ( tu , range ) ;
121
121
var tokenIt = new TokenIterator ( tokenizer ) ;
@@ -134,7 +134,7 @@ public static void ParseAttributesInRange(CXTranslationUnit tu, CXSourceRange ra
134
134
135
135
while ( tokenIt . CanPeek )
136
136
{
137
- if ( ParseAttributes ( tokenIt , ref collectAttributes ) )
137
+ if ( ParseAttributes ( globalContainer , tokenIt , ref collectAttributes ) )
138
138
{
139
139
continue ;
140
140
}
@@ -615,17 +615,17 @@ bool HasInlineTypeDefinition(CXCursor varDecl)
615
615
CXSourceLocation GetNextLocation ( CXSourceLocation loc , int inc = 1 )
616
616
{
617
617
CXSourceLocation value ;
618
- loc . GetSpellingLocation ( out var f , out var u , out var z , out var originalOffset ) ;
619
- var offset = IncOffset ( inc , z ) ;
620
- var shouldUseLine = ( z != 0 && ( offset != 0 || offset != uint . MaxValue ) ) ;
618
+ loc . GetSpellingLocation ( out var file , out var line , out var column , out var originalOffset ) ;
619
+ var signedOffset = ( int ) column + inc ;
620
+ var shouldUseLine = ( column != 0 && signedOffset > 0 ) ;
621
621
if ( shouldUseLine )
622
622
{
623
- value = tu . GetLocation ( f , u , offset ) ;
623
+ value = tu . GetLocation ( file , line , ( uint ) signedOffset ) ;
624
624
}
625
625
else
626
626
{
627
- offset = IncOffset ( inc , originalOffset ) ;
628
- value = tu . GetLocationForOffset ( f , offset ) ;
627
+ var offset = IncOffset ( inc , originalOffset ) ;
628
+ value = tu . GetLocationForOffset ( file , offset ) ;
629
629
}
630
630
631
631
return value ;
@@ -911,8 +911,42 @@ private enum AttributeLexerParseStatus
911
911
Error ,
912
912
}
913
913
914
+ private static ( string , string ) GetNameSpaceAndAttribute ( string fullAttribute )
915
+ {
916
+ string [ ] colons = { "::" } ;
917
+ string [ ] tokens = fullAttribute . Split ( colons , System . StringSplitOptions . None ) ;
918
+ if ( tokens . Length == 2 )
919
+ {
920
+ return ( tokens [ 0 ] , tokens [ 1 ] ) ;
921
+ }
922
+ else
923
+ {
924
+ return ( null , tokens [ 0 ] ) ;
925
+ }
926
+ }
927
+
928
+
929
+ private static ( string , string ) GetNameAndArguments ( string name )
930
+ {
931
+ if ( name . Contains ( "(" ) )
932
+ {
933
+ Char [ ] seperator = { '(' } ;
934
+ var argumentTokens = name . Split ( seperator , 2 ) ;
935
+ var length = argumentTokens [ 1 ] . LastIndexOf ( ')' ) ;
936
+ string argument = null ;
937
+ if ( length > 0 )
938
+ {
939
+ argument = argumentTokens [ 1 ] . Substring ( 0 , length ) ;
940
+ }
941
+ return ( argumentTokens [ 0 ] , argument ) ;
942
+ }
943
+ else
944
+ {
945
+ return ( name , null ) ;
946
+ }
947
+ }
914
948
915
- private static bool ParseAttributes ( TokenIterator tokenIt , ref List < CppAttribute > attributes )
949
+ private static bool ParseAttributes ( CppGlobalDeclarationContainer globalContainer , TokenIterator tokenIt , ref List < CppAttribute > attributes )
916
950
{
917
951
// Parse C++ attributes
918
952
// [[<attribute>]]
@@ -985,6 +1019,32 @@ private static bool ParseAttributes(TokenIterator tokenIt, ref List<CppAttribute
985
1019
return tokenIt . Skip ( ")" ) ; ;
986
1020
}
987
1021
1022
+ // See if we have a macro
1023
+ var value = tokenIt . PeekText ( ) ;
1024
+ var macro = globalContainer . Macros . Find ( v => v . Name == value ) ;
1025
+ if ( macro != null )
1026
+ {
1027
+ if ( macro . Value . StartsWith ( "[[" ) && macro . Value . EndsWith ( "]]" ) )
1028
+ {
1029
+ CppAttribute attribute = null ;
1030
+ var fullAttribute = macro . Value . Substring ( 2 , macro . Value . Length - 4 ) ;
1031
+ var ( scope , name ) = GetNameSpaceAndAttribute ( fullAttribute ) ;
1032
+ var ( attributeName , arguments ) = GetNameAndArguments ( name ) ;
1033
+
1034
+ attribute = new CppAttribute ( attributeName , AttributeKind . TokenAttribute ) ;
1035
+ attribute . Scope = scope ;
1036
+ attribute . Arguments = arguments ;
1037
+
1038
+ if ( attributes == null )
1039
+ {
1040
+ attributes = new List < CppAttribute > ( ) ;
1041
+ }
1042
+ attributes . Add ( attribute ) ;
1043
+ tokenIt . Next ( ) ;
1044
+ return true ;
1045
+ }
1046
+ }
1047
+
988
1048
return false ;
989
1049
}
990
1050
0 commit comments