[clang] 161fc1d - [Fixed Point] [AST] Add an AST serialization code for fixed-point literals.
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 14 11:21:12 PDT 2020
Author: Vince Bridgers
Date: 2020-04-14T13:20:49-05:00
New Revision: 161fc1d9118f4f64887cf7845a51ec79f1a8602f
URL: https://github.com/llvm/llvm-project/commit/161fc1d9118f4f64887cf7845a51ec79f1a8602f
DIFF: https://github.com/llvm/llvm-project/commit/161fc1d9118f4f64887cf7845a51ec79f1a8602f.diff
LOG: [Fixed Point] [AST] Add an AST serialization code for fixed-point literals.
Summary:
This patch adds the EXPR_FIXEDPOINT_LITERAL AST
code to serialize FixedPointLiterals. They were previously
being serialized with the code for integer literals, which
doesn't work properly.
Reviewers: leonardchan, rjmccall
Reviewed By: leonardchan, rjmccall
Subscribers: vabridgers, JesperAntonsson, bjope, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57226
Added:
clang/test/PCH/Inputs/fixed-point-literal.h
clang/test/PCH/fixed-point-literal.c
Modified:
clang/include/clang/AST/Expr.h
clang/include/clang/Serialization/ASTBitCodes.h
clang/lib/AST/Expr.cpp
clang/lib/Serialization/ASTReaderStmt.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/lib/Serialization/ASTWriterStmt.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index e566e3c6ab89..c1ef6ed09ce8 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1484,7 +1484,7 @@ class FixedPointLiteral : public Expr, public APIntStorage {
SourceLocation Loc;
unsigned Scale;
- /// \brief Construct an empty integer literal.
+ /// \brief Construct an empty fixed-point literal.
explicit FixedPointLiteral(EmptyShell Empty)
: Expr(FixedPointLiteralClass, Empty) {}
@@ -1498,6 +1498,9 @@ class FixedPointLiteral : public Expr, public APIntStorage {
QualType type, SourceLocation l,
unsigned Scale);
+ /// Returns an empty fixed-point literal.
+ static FixedPointLiteral *Create(const ASTContext &C, EmptyShell Empty);
+
SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
@@ -1506,6 +1509,9 @@ class FixedPointLiteral : public Expr, public APIntStorage {
void setLocation(SourceLocation Location) { Loc = Location; }
+ unsigned getScale() const { return Scale; }
+ void setScale(unsigned S) { Scale = S; }
+
static bool classof(const Stmt *T) {
return T->getStmtClass() == FixedPointLiteralClass;
}
diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h
index e6e9c8570cc8..b4143c74b16a 100644
--- a/clang/include/clang/Serialization/ASTBitCodes.h
+++ b/clang/include/clang/Serialization/ASTBitCodes.h
@@ -1887,6 +1887,9 @@ namespace serialization {
EXPR_COAWAIT,
EXPR_COYIELD,
EXPR_DEPENDENT_COAWAIT,
+
+ // FixedPointLiteral
+ EXPR_FIXEDPOINT_LITERAL,
};
/// The kinds of designators that can occur in a
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index a1e66c55b336..46ecba29ef19 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -913,6 +913,11 @@ FixedPointLiteral *FixedPointLiteral::CreateFromRawInt(const ASTContext &C,
return new (C) FixedPointLiteral(C, V, type, l, Scale);
}
+FixedPointLiteral *FixedPointLiteral::Create(const ASTContext &C,
+ EmptyShell Empty) {
+ return new (C) FixedPointLiteral(Empty);
+}
+
std::string FixedPointLiteral::getValueAsString(unsigned Radix) const {
// Currently the longest decimal number that can be printed is the max for an
// unsigned long _Accum: 4294967295.99999999976716935634613037109375
diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp
index ce9ee292c7c9..f2338a1228df 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -605,6 +605,7 @@ void ASTStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
void ASTStmtReader::VisitFixedPointLiteral(FixedPointLiteral *E) {
VisitExpr(E);
E->setLocation(readSourceLocation());
+ E->setScale(Record.readInt());
E->setValue(Record.getContext(), Record.readAPInt());
}
@@ -2857,6 +2858,10 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
S = IntegerLiteral::Create(Context, Empty);
break;
+ case EXPR_FIXEDPOINT_LITERAL:
+ S = FixedPointLiteral::Create(Context, Empty);
+ break;
+
case EXPR_FLOATING_LITERAL:
S = FloatingLiteral::Create(Context, Empty);
break;
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 3b72f0f1cef2..6fb2357558f6 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -571,6 +571,7 @@ static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
RECORD(EXPR_PREDEFINED);
RECORD(EXPR_DECL_REF);
RECORD(EXPR_INTEGER_LITERAL);
+ RECORD(EXPR_FIXEDPOINT_LITERAL);
RECORD(EXPR_FLOATING_LITERAL);
RECORD(EXPR_IMAGINARY_LITERAL);
RECORD(EXPR_STRING_LITERAL);
diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp
index 9ad68d6bc05a..06a00790aadd 100644
--- a/clang/lib/Serialization/ASTWriterStmt.cpp
+++ b/clang/lib/Serialization/ASTWriterStmt.cpp
@@ -629,8 +629,9 @@ void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
void ASTStmtWriter::VisitFixedPointLiteral(FixedPointLiteral *E) {
VisitExpr(E);
Record.AddSourceLocation(E->getLocation());
+ Record.push_back(E->getScale());
Record.AddAPInt(E->getValue());
- Code = serialization::EXPR_INTEGER_LITERAL;
+ Code = serialization::EXPR_FIXEDPOINT_LITERAL;
}
void ASTStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
diff --git a/clang/test/PCH/Inputs/fixed-point-literal.h b/clang/test/PCH/Inputs/fixed-point-literal.h
new file mode 100644
index 000000000000..57022ce5c6e2
--- /dev/null
+++ b/clang/test/PCH/Inputs/fixed-point-literal.h
@@ -0,0 +1,5 @@
+// Header for PCH test fixed-point-literal.c
+
+const short _Fract sf = -0.25r;
+const _Fract f = 0.75r;
+const long _Accum la = 25.25lk;
diff --git a/clang/test/PCH/fixed-point-literal.c b/clang/test/PCH/fixed-point-literal.c
new file mode 100644
index 000000000000..996d8c117089
--- /dev/null
+++ b/clang/test/PCH/fixed-point-literal.c
@@ -0,0 +1,15 @@
+
+// Test this without pch.
+// RUN: %clang_cc1 -ffixed-point -include %S/Inputs/fixed-point-literal.h -fsyntax-only -ast-print -o - %s | FileCheck %s
+
+// Test with pch.
+// RUN: %clang_cc1 -ffixed-point -emit-pch -o %t %S/Inputs/fixed-point-literal.h
+// RUN: %clang_cc1 -ffixed-point -include-pch %t -fsyntax-only -ast-print -o - %s | FileCheck %s
+
+// CHECK: const short _Fract sf = -0.25r;
+// CHECK: const _Fract f = 0.75r;
+// CHECK: const long _Accum la = 25.25lk;
+
+short _Fract sf2 = sf;
+_Fract f2 = f;
+long _Accum la2 = la;
More information about the cfe-commits
mailing list