forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CXX: add constexpr to "properties:" field
Partially close universal-ctags#3539. Signed-off-by: Masatake YAMATO <yamato@redhat.com>
- Loading branch information
Showing
9 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--sort=no | ||
--kinds-c++=* | ||
--fields=+x | ||
--fields-c++=+{properties} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
factorial input.cc /^constexpr int factorial(int n)$/;" f typeref:typename:int properties:constexpr | ||
n input.cc /^constexpr int factorial(int n)$/;" z function:factorial typeref:typename:int file: | ||
factorial_cxx14 input.cc /^constexpr int factorial_cxx14(int n)$/;" f typeref:typename:int properties:constexpr | ||
n input.cc /^constexpr int factorial_cxx14(int n)$/;" z function:factorial_cxx14 typeref:typename:int file: | ||
res input.cc /^ int res = 1;$/;" l function:factorial_cxx14 typeref:typename:int file: | ||
conststr input.cc /^class conststr$/;" c file: | ||
p input.cc /^ const char* p;$/;" m class:conststr typeref:typename:const char * file: | ||
sz input.cc /^ std::size_t sz;$/;" m class:conststr typeref:typename:std::size_t file: | ||
conststr input.cc /^ constexpr conststr(const char(&a)[N]): p(a), sz(N - 1) {}$/;" f class:conststr file: properties:constexpr | ||
a input.cc /^ constexpr conststr(const char(&a)[N]): p(a), sz(N - 1) {}$/;" z function:conststr::conststr typeref:typename:const char (&)[N] file: | ||
operator [] input.cc /^ constexpr char operator[](std::size_t n) const$/;" f class:conststr typeref:typename:char file: properties:const,constexpr | ||
n input.cc /^ constexpr char operator[](std::size_t n) const$/;" z function:conststr::operator [] typeref:typename:std::size_t file: | ||
size input.cc /^ constexpr std::size_t size() const { return sz; }$/;" f class:conststr typeref:typename:std::size_t file: properties:const,constexpr | ||
countlower input.cc /^constexpr std::size_t countlower(conststr s, std::size_t n = 0,$/;" f typeref:typename:std::size_t properties:constexpr | ||
s input.cc /^constexpr std::size_t countlower(conststr s, std::size_t n = 0,$/;" z function:countlower typeref:typename:conststr file: | ||
n input.cc /^constexpr std::size_t countlower(conststr s, std::size_t n = 0,$/;" z function:countlower typeref:typename:std::size_t file: | ||
c input.cc /^ std::size_t c = 0)$/;" z function:countlower typeref:typename:std::size_t file: | ||
constN input.cc /^struct constN$/;" s file: | ||
n input.cc /^template<int n>$/;" Z struct:constN typeref:typename:int | ||
constN input.cc /^ constN() { std::cout << n << '\\n'; }$/;" f struct:constN file: | ||
main input.cc /^int main()$/;" f typeref:typename:int | ||
out1 input.cc /^ constN<factorial(4)> out1; \/\/ computed at compile time$/;" l function:main typeref:typename:constN<factorial (4)> file: | ||
k input.cc /^ volatile int k = 8; \/\/ disallow optimization using volatile$/;" l function:main typeref:typename:volatile int file: | ||
out2 input.cc /^ constN<countlower("Hello, world!")> out2; \/\/ implicitly converted to conststr$/;" l function:main typeref:typename:constN<countlower ("Hello, world!")> file: | ||
a input.cc /^ constexpr int a[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8};$/;" l function:main typeref:typename:int[12] file: properties:constexpr | ||
length_a input.cc /^ constexpr int length_a = sizeof(a)\/sizeof(int); \/\/ std::size(a) in C++17,$/;" l function:main typeref:typename:int file: properties:constexpr | ||
i input.cc /^ for (int i = 0; i < length_a; ++i)$/;" l function:main typeref:typename:int file: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Taken from https://en.cppreference.com/w/cpp/language/constexpr | ||
|
||
#include <iostream> | ||
#include <stdexcept> | ||
|
||
// C++11 constexpr functions use recursion rather than iteration | ||
constexpr int factorial(int n) | ||
{ | ||
return n <= 1 ? 1 : (n * factorial(n - 1)); | ||
} | ||
|
||
// C++14 constexpr functions may use local variables and loops | ||
#if __cplusplus >= 201402L | ||
constexpr int factorial_cxx14(int n) | ||
{ | ||
int res = 1; | ||
while (n > 1) | ||
res *= n--; | ||
return res; | ||
} | ||
#endif // C++14 | ||
|
||
// literal class | ||
class conststr | ||
{ | ||
const char* p; | ||
std::size_t sz; | ||
public: | ||
template<std::size_t N> | ||
constexpr conststr(const char(&a)[N]): p(a), sz(N - 1) {} | ||
|
||
// constexpr functions signal errors by throwing exceptions | ||
// in C++11, they must do so from the conditional operator ?: | ||
constexpr char operator[](std::size_t n) const | ||
{ | ||
return n < sz ? p[n] : throw std::out_of_range(""); | ||
} | ||
|
||
constexpr std::size_t size() const { return sz; } | ||
}; | ||
|
||
// C++11 constexpr functions had to put everything in a single return statement | ||
// (C++14 doesn't have that requirement) | ||
constexpr std::size_t countlower(conststr s, std::size_t n = 0, | ||
std::size_t c = 0) | ||
{ | ||
return n == s.size() ? c : | ||
'a' <= s[n] && s[n] <= 'z' ? countlower(s, n + 1, c + 1) : | ||
countlower(s, n + 1, c); | ||
} | ||
|
||
// output function that requires a compile-time constant, for testing | ||
template<int n> | ||
struct constN | ||
{ | ||
constN() { std::cout << n << '\n'; } | ||
}; | ||
|
||
int main() | ||
{ | ||
std::cout << "4! = " ; | ||
constN<factorial(4)> out1; // computed at compile time | ||
|
||
volatile int k = 8; // disallow optimization using volatile | ||
std::cout << k << "! = " << factorial(k) << '\n'; // computed at run time | ||
|
||
std::cout << "the number of lowercase letters in \"Hello, world!\" is "; | ||
constN<countlower("Hello, world!")> out2; // implicitly converted to conststr | ||
|
||
constexpr int a[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; | ||
constexpr int length_a = sizeof(a)/sizeof(int); // std::size(a) in C++17, | ||
// std::ssize(a) in C++20 | ||
std::cout << "array of length " << length_a << " has elements: "; | ||
for (int i = 0; i < length_a; ++i) | ||
std::cout << a[i] << " "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters