aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Tooling/Syntax/BuildTree.cpp
diff options
context:
space:
mode:
authorEduardo Caldas <ecaldas@google.com>2020-07-09 15:49:15 +0000
committerEduardo Caldas <ecaldas@google.com>2020-07-10 16:21:11 +0000
commit1db5b348c4c93b6610afb4fd515b389989efc302 (patch)
treed2e54377e6d3de33c1d911fcc8bd1ba8341eee50 /clang/lib/Tooling/Syntax/BuildTree.cpp
parentf33c2c27a8d4ea831aa7c2c2649066be91318d85 (diff)
downloadllvm-1db5b348c4c93b6610afb4fd515b389989efc302.zip
llvm-1db5b348c4c93b6610afb4fd515b389989efc302.tar.gz
llvm-1db5b348c4c93b6610afb4fd515b389989efc302.tar.bz2
Add kinded UDL for raw literal operator and numeric literal operator template
Diffstat (limited to 'clang/lib/Tooling/Syntax/BuildTree.cpp')
-rw-r--r--clang/lib/Tooling/Syntax/BuildTree.cpp45
1 files changed, 30 insertions, 15 deletions
diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp
index 8204d3f..5afe496 100644
--- a/clang/lib/Tooling/Syntax/BuildTree.cpp
+++ b/clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -23,6 +23,7 @@
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Lexer.h"
+#include "clang/Lex/LiteralSupport.h"
#include "clang/Tooling/Syntax/Nodes.h"
#include "clang/Tooling/Syntax/Tokens.h"
#include "clang/Tooling/Syntax/Tree.h"
@@ -552,8 +553,8 @@ private:
namespace {
class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
public:
- explicit BuildTreeVisitor(ASTContext &Ctx, syntax::TreeBuilder &Builder)
- : Builder(Builder), LangOpts(Ctx.getLangOpts()) {}
+ explicit BuildTreeVisitor(ASTContext &Context, syntax::TreeBuilder &Builder)
+ : Builder(Builder), Context(Context) {}
bool shouldTraversePostOrder() const { return true; }
@@ -718,30 +719,44 @@ public:
return WalkUpFromUserDefinedLiteral(S);
}
- syntax::NodeKind getUserDefinedLiteralKind(UserDefinedLiteral *S) {
+ syntax::UserDefinedLiteralExpression *
+ buildUserDefinedLiteral(UserDefinedLiteral *S) {
switch (S->getLiteralOperatorKind()) {
case clang::UserDefinedLiteral::LOK_Integer:
- return syntax::NodeKind::IntegerUserDefinedLiteralExpression;
+ return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
case clang::UserDefinedLiteral::LOK_Floating:
- return syntax::NodeKind::FloatUserDefinedLiteralExpression;
+ return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
case clang::UserDefinedLiteral::LOK_Character:
- return syntax::NodeKind::CharUserDefinedLiteralExpression;
+ return new (allocator()) syntax::CharUserDefinedLiteralExpression;
case clang::UserDefinedLiteral::LOK_String:
- return syntax::NodeKind::StringUserDefinedLiteralExpression;
+ return new (allocator()) syntax::StringUserDefinedLiteralExpression;
case clang::UserDefinedLiteral::LOK_Raw:
case clang::UserDefinedLiteral::LOK_Template:
- // FIXME: Apply `NumericLiteralParser` to the underlying token to deduce
- // the right UDL kind. That would require a `Preprocessor` though.
- return syntax::NodeKind::UnknownUserDefinedLiteralExpression;
+ // For raw literal operator and numeric literal operator template we
+ // cannot get the type of the operand in the semantic AST. We get this
+ // information from the token. As integer and floating point have the same
+ // token kind, we run `NumericLiteralParser` again to distinguish them.
+ auto TokLoc = S->getBeginLoc();
+ auto buffer = SmallVector<char, 16>();
+ bool invalidSpelling = false;
+ auto TokSpelling =
+ Lexer::getSpelling(TokLoc, buffer, Context.getSourceManager(),
+ Context.getLangOpts(), &invalidSpelling);
+ assert(!invalidSpelling);
+ auto Literal =
+ NumericLiteralParser(TokSpelling, TokLoc, Context.getSourceManager(),
+ Context.getLangOpts(), Context.getTargetInfo(),
+ Context.getDiagnostics());
+ if (Literal.isIntegerLiteral())
+ return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
+ else
+ return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
}
}
bool WalkUpFromUserDefinedLiteral(UserDefinedLiteral *S) {
Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
- Builder.foldNode(Builder.getExprRange(S),
- new (allocator()) syntax::UserDefinedLiteralExpression(
- getUserDefinedLiteralKind(S)),
- S);
+ Builder.foldNode(Builder.getExprRange(S), buildUserDefinedLiteral(S), S);
return true;
}
@@ -1262,7 +1277,7 @@ private:
llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); }
syntax::TreeBuilder &Builder;
- const LangOptions &LangOpts;
+ const ASTContext &Context;
};
} // namespace