Skip to content

Commit

Permalink
Merge pull request #3602 from masatake/c++--consteval
Browse files Browse the repository at this point in the history
C++,C: record consteval, constinit, thread_local, and __thread to properties: field
  • Loading branch information
masatake committed Dec 18, 2022
2 parents b2588fe + caf1bd5 commit bec7905
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Units/parser-c.r/properties-thread.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--sort=no
--kinds-c=*-{parameter}
--fields=+x
--fields-c=+{properties}
3 changes: 3 additions & 0 deletions Units/parser-c.r/properties-thread.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
i input.c /^__thread int i;$/;" v typeref:typename:int properties:thread_local
s input.c /^extern __thread struct state s;$/;" x typeref:struct:state properties:extern,thread_local
p input.c /^static __thread char *p;$/;" v typeref:typename:char * file: properties:static,thread_local
4 changes: 4 additions & 0 deletions Units/parser-c.r/properties-thread.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Taken from https://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html#Thread-Local */
__thread int i;
extern __thread struct state s;
static __thread char *p;
4 changes: 4 additions & 0 deletions Units/parser-cxx.r/properties-consteval.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--sort=no
--kinds-c++=*-{parameter}
--fields=+x
--fields-c++=+{properties}
9 changes: 9 additions & 0 deletions Units/parser-cxx.r/properties-consteval.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sqr input.cc /^consteval int sqr(int n)$/;" f typeref:typename:int properties:consteval
r input.cc /^constexpr int r = sqr(100); \/\/ OK$/;" v typeref:typename:int properties:constexpr
x input.cc /^int x = 100;$/;" v typeref:typename:int
r2 input.cc /^int r2 = sqr(x); \/\/ Error: Call does not produce a constant$/;" v typeref:typename:int
sqrsqr input.cc /^consteval int sqrsqr(int n)$/;" f typeref:typename:int properties:consteval
dblsqr input.cc /^constexpr int dblsqr(int n)$/;" f typeref:typename:int properties:constexpr
f input.cc /^consteval int f() { return 42; }$/;" f typeref:typename:int properties:consteval
g input.cc /^consteval auto g() { return &f; }$/;" f typeref:typename:auto properties:consteval
h input.cc /^consteval int h(int (*p)() = g()) { return p(); }$/;" f typeref:typename:int properties:consteval
25 changes: 25 additions & 0 deletions Units/parser-cxx.r/properties-consteval.d/input.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Taken from https://en.cppreference.com/w/cpp/language/consteval
consteval int sqr(int n)
{
return n*n;
}
constexpr int r = sqr(100); // OK

int x = 100;
int r2 = sqr(x); // Error: Call does not produce a constant

consteval int sqrsqr(int n)
{
return sqr(sqr(n)); // Not a constant expression at this point, but OK
}

constexpr int dblsqr(int n)
{
return 2 * sqr(n); // Error: Enclosing function is not consteval
// and sqr(n) is not a constant
}

consteval int f() { return 42; }
consteval auto g() { return &f; }
consteval int h(int (*p)() = g()) { return p(); }

4 changes: 4 additions & 0 deletions Units/parser-cxx.r/properties-constinit.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--sort=no
--kinds-c++=*-{parameter}
--fields=+x
--fields-c++=+{properties}
5 changes: 5 additions & 0 deletions Units/parser-cxx.r/properties-constinit.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
g input.cc /^const char *g() { return "dynamic initialization"; }$/;" f typeref:typename:const char *
f input.cc /^constexpr const char *f(bool p) { return p ? "constant initializer" : g(); }$/;" f typeref:typename:const char * properties:constexpr
c input.cc /^constinit const char *c = f(true); \/\/ OK$/;" v typeref:typename:const char * properties:constinit
x input.cc /^extern thread_local constinit int x;$/;" x typeref:typename:int properties:extern,constinit,thread_local
f input.cc /^int f() { return x; } \/\/ no check of a guard variable needed$/;" f typeref:typename:int
8 changes: 8 additions & 0 deletions Units/parser-cxx.r/properties-constinit.d/input.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Taken from https://en.cppreference.com/w/cpp/language/constinit
const char *g() { return "dynamic initialization"; }
constexpr const char *f(bool p) { return p ? "constant initializer" : g(); }

constinit const char *c = f(true); // OK

extern thread_local constinit int x;
int f() { return x; } // no check of a guard variable needed
17 changes: 16 additions & 1 deletion parsers/cxx/cxx_keyword.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ static CXXKeywordDescriptor g_aCXXKeywordTable[] = {
CXXLanguageCPP,
0
},
{
"__thread",
CXXLanguageC | CXXLanguageCPP,
CXXKeywordMayAppearInVariableDeclaration | CXXKeywordExcludeFromTypeNames,
},
{
"alignas",
CXXLanguageCPP,
Expand Down Expand Up @@ -212,11 +217,21 @@ static CXXKeywordDescriptor g_aCXXKeywordTable[] = {
CXXLanguageC | CXXLanguageCPP | CXXLanguageCUDA,
CXXKeywordMayAppearInVariableDeclaration | CXXKeywordFlagMayBePartOfTypeName
},
{
"consteval",
CXXLanguageCPP,
CXXKeywordExcludeFromTypeNames
},
{
"constexpr",
CXXLanguageCPP,
CXXKeywordMayAppearInVariableDeclaration | CXXKeywordExcludeFromTypeNames
},
{
"constinit",
CXXLanguageCPP,
CXXKeywordMayAppearInVariableDeclaration | CXXKeywordExcludeFromTypeNames
},
{
"const_cast",
CXXLanguageCPP,
Expand Down Expand Up @@ -465,7 +480,7 @@ static CXXKeywordDescriptor g_aCXXKeywordTable[] = {
{
"thread_local",
CXXLanguageCPP,
CXXKeywordMayAppearInVariableDeclaration
CXXKeywordMayAppearInVariableDeclaration | CXXKeywordExcludeFromTypeNames
},
{
"throw",
Expand Down
5 changes: 4 additions & 1 deletion parsers/cxx/cxx_keyword.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef enum _CXXKeyword
CXXKeyword__SHARED__, // CUDA
CXXKeyword__STDCALL, // Microsoft C/C++
CXXKeyword__THISCALL, // Microsoft C/C++
CXXKeyword__THREAD, // GCC (https://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html#Thread-Local)
CXXKeywordALIGNAS, // (since C++11)
CXXKeywordALIGNOF, // (since C++11)
//CXXKeywordAND,
Expand All @@ -52,7 +53,9 @@ typedef enum _CXXKeyword
//CXXKeywordCOMPL,
CXXKeywordCONCEPT, // Concepts TS
CXXKeywordCONST,
CXXKeywordCONSTEVAL, // (since C++20)
CXXKeywordCONSTEXPR, // (since C++11)
CXXKeywordCONSTINIT, // (since C++20)
CXXKeywordCONST_CAST,
CXXKeywordCONTINUE,
CXXKeywordDECLTYPE, // (since C++11)
Expand Down Expand Up @@ -175,4 +178,4 @@ void cxxKeywordEnableFinal(bool bEnableIt);
bool cxxKeywordIsDisabled(CXXKeyword eKeywordId);


#endif //!ctags_cxx_keyword_h_
#endif //!ctags_cxx_keyword_h_
12 changes: 12 additions & 0 deletions parsers/cxx/cxx_parser_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ static bool cxxParserParseBlockInternal(bool bExpectClosingBracket)
break;
case CXXKeywordCONSTEXPR:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConstexpr;
break;
case CXXKeywordCONSTEVAL:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConsteval;
break;
case CXXKeywordCONSTINIT:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConstinit;
break;
case CXXKeywordTHREAD_LOCAL:
case CXXKeyword__THREAD:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenThreadLocal;
break;

default:
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenTypedef)
{
Expand Down
3 changes: 3 additions & 0 deletions parsers/cxx/cxx_parser_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,9 @@ int cxxParserEmitFunctionTags(
uProperties |= CXXTagPropertyDeprecated;
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenConstexpr)
uProperties |= CXXTagPropertyConstexpr;
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenConsteval)
uProperties |= CXXTagPropertyConsteval;
// constinit is not here; it is for variables.
if(pInfo->pSignatureConst)
uProperties |= CXXTagPropertyConst;
if(pInfo->uFlags & CXXFunctionSignatureInfoPure)
Expand Down
6 changes: 6 additions & 0 deletions parsers/cxx/cxx_parser_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ typedef enum _CXXParserKeywordState
CXXParserKeywordStateSeenFriend = (1 << 12),
// "constexpr" has been seen
CXXParserKeywordStateSeenConstexpr = (1 << 13),
// "consteval" has been seen
CXXParserKeywordStateSeenConsteval = (1 << 14),
// "constinit" has been seen
CXXParserKeywordStateSeenConstinit = (1 << 15),
// "thread_local" has been seen
CXXParserKeywordStateSeenThreadLocal = (1 << 16),
} CXXParserKeywordState;

#define CXX_PARSER_MAXIMUM_NESTING_LEVELS 1024
Expand Down
5 changes: 5 additions & 0 deletions parsers/cxx/cxx_parser_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,11 @@ bool cxxParserExtractVariableDeclarations(CXXTokenChain * pChain,unsigned int uF
// uProperties |= CXXTagPropertyVolatile;
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenConstexpr)
uProperties |= CXXTagPropertyConstexpr;
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenConstinit)
uProperties |= CXXTagPropertyConstinit;
// consteval is not here; it is for functions.
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenThreadLocal)
uProperties |= CXXTagPropertyThreadLocal;

pszProperties = cxxTagSetProperties(uProperties);
}
Expand Down
6 changes: 6 additions & 0 deletions parsers/cxx/cxx_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ vString * cxxTagSetProperties(unsigned int uProperties)
ADD_PROPERTY("fntryblock");
if (uProperties & CXXTagPropertyConstexpr)
ADD_PROPERTY("constexpr");
if (uProperties & CXXTagPropertyConsteval)
ADD_PROPERTY("consteval");
if (uProperties & CXXTagPropertyConstinit)
ADD_PROPERTY("constinit");
if (uProperties & CXXTagPropertyThreadLocal)
ADD_PROPERTY("thread_local");

cxxTagSetField(CXXTagFieldProperties,vStringValue(pszProperties),false);

Expand Down
6 changes: 6 additions & 0 deletions parsers/cxx/cxx_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ typedef enum _CXXTagProperty
CXXTagPropertyFunctionTryBlock = (1 << 17),
// constexpr has been seen.
CXXTagPropertyConstexpr = (1 << 18),
// consteval has been seen.
CXXTagPropertyConsteval = (1 << 19),
// constinit has been seen.
CXXTagPropertyConstinit = (1 << 20),
// thread_local has been seen.
CXXTagPropertyThreadLocal = (1 << 21),
} CXXTagProperty;

// Set the modifiers field of the tag.
Expand Down

0 comments on commit bec7905

Please sign in to comment.