public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/c++-contracts] c++: Cleanup get_source; use obstack
@ 2021-07-06 20:43 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-07-06 20:43 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:fd571461f81f903721453d5f4ce245269fcc34e7
commit fd571461f81f903721453d5f4ce245269fcc34e7
Author: Jeff Chapman II <jchapman@lock3software.com>
Date: Fri May 21 08:05:03 2021 -0400
c++: Cleanup get_source; use obstack
gcc/
* input.c (get_source): Simplify; use obstack instead of a fixed
sized static buffer.
gcc/testsuite/
* g++.dg/cpp2a/contracts-multiline1.C: New.
Diff:
---
gcc/input.c | 28 ++++++++---------------
gcc/testsuite/g++.dg/cpp2a/contracts-multiline1.C | 19 +++++++++++++++
2 files changed, 29 insertions(+), 18 deletions(-)
diff --git a/gcc/input.c b/gcc/input.c
index 7704bd64607..8cd04962515 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -865,20 +865,18 @@ get_source (location_t start, location_t end)
if (line.length () < 1)
return NULL;
int s = expstart.column - 1;
- int l = expend.column - expstart.column + 1;
- if (line.length () < (size_t)s + l)
+ int l = expend.column - s;
+ if (line.length () < (size_t)expend.column)
return NULL;
return line.subspan (s, l).xstrdup ();
}
- /* FIXME how should we handle newlines and runs of spaces? */
- char buf[1024 + 4]{};
- char *res = buf;
- size_t len = 1024;
+ struct obstack buf_obstack;
+ obstack_init (&buf_obstack);
/* Loop through all lines in the range and append each to buf; may trim
parts of the start and end lines off depending on column values. */
- for (int l = expstart.line; len > 0 && l <= expend.line; ++l)
+ for (int l = expstart.line; l <= expend.line; ++l)
{
char_span line = location_get_source_line (expstart.file, l);
if (line.length () < 1 && (l != expstart.line && l != expend.line))
@@ -902,20 +900,14 @@ get_source (location_t start, location_t end)
line = line.subspan (0, expend.column);
}
- /* if we've run out of buffer, truncate the line */
- if (line.length() >= len)
- line = line.subspan (0, len);
-
- gcc_assert (line.length () <= len);
- strncat (res, line.get_buffer (), line.length ());
- res += line.length ();
- len -= line.length ();
+ obstack_grow (&buf_obstack, line.get_buffer (), line.length ());
}
- /* If we ran out of space, add a '...' abbreviation marker. */
- if (len <= 0)
- buf[1024] = buf[1025] = buf[1026] = '.';
+ /* Null terminate and finish the buf obstack. */
+ obstack_1grow (&buf_obstack, 0);
+ const char *buf = (const char *) obstack_finish (&buf_obstack);
+ /* TODO should we collapse/trim newlines and runs of spaces? */
return xstrdup (buf);
}
diff --git a/gcc/testsuite/g++.dg/cpp2a/contracts-multiline1.C b/gcc/testsuite/g++.dg/cpp2a/contracts-multiline1.C
new file mode 100644
index 00000000000..e07c4a94d9a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/contracts-multiline1.C
@@ -0,0 +1,19 @@
+// { dg-do run }
+// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
+
+int main(int, char **)
+{
+ int x = 5;
+ int y = 10;
+ [[ assert:
+ x
+ <
+ 10
+ &&
+ y
+ >
+ 123
+ ]];
+}
+
+// { dg-output "default std::handle_contract_violation called: .*.C 8 main x.*10.*y.*123.*(\n|\r\n|\r)*" }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-07-06 20:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-06 20:43 [gcc/devel/c++-contracts] c++: Cleanup get_source; use obstack Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).