summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter klausler <pklausler@nvidia.com>2018-07-25 15:13:40 -0700
committerpeter klausler <pklausler@nvidia.com>2018-08-03 16:23:57 -0700
commit439ddb334f2da162fe465aaeb45b670eb5e3ef13 (patch)
tree8575f72fb10c2bbfa6eb89673057b455713190bb
parentb861018e3a1bda307b8ebe6724eb144e74e3aba6 (diff)
downloadllvm-439ddb334f2da162fe465aaeb45b670eb5e3ef13.tar.gz
[flang] Use std::nullopt in place of empty braces in return statements.
Original-commit: flang-compiler/f18@90e556301c47c953e13900974dd5175d07bcc8ad Reviewed-on: https://github.com/flang-compiler/f18/pull/162 Tree-same-pre-rewrite: false
-rw-r--r--flang/documentation/C++style.md8
-rw-r--r--flang/lib/parser/basic-parsers.h48
-rw-r--r--flang/lib/parser/characters.cc6
-rw-r--r--flang/lib/parser/characters.h4
-rw-r--r--flang/lib/parser/instrumented-parser.h2
-rw-r--r--flang/lib/parser/parse-state.h4
-rw-r--r--flang/lib/parser/preprocessor.cc2
-rw-r--r--flang/lib/parser/prescan.cc16
-rw-r--r--flang/lib/parser/token-parsers.h66
-rw-r--r--flang/lib/parser/user-state.cc4
-rw-r--r--flang/lib/semantics/expression.cc10
11 files changed, 87 insertions, 83 deletions
diff --git a/flang/documentation/C++style.md b/flang/documentation/C++style.md
index 2b6cef20fc2a..6bdac9549c77 100644
--- a/flang/documentation/C++style.md
+++ b/flang/documentation/C++style.md
@@ -102,11 +102,14 @@ you are considering.
1. Never use run-time type information or `dynamic_cast<>`.
1. Never declare static data that executes a constructor.
(This is why `#include <iostream>` is contraindicated.)
-Use `{braced initializers}` in all circumstances where they work, including
+1. Use `{braced initializers}` in all circumstances where they work, including
default data member initialization. They inhibit implicit truncation.
Don't use `= expr` initialization just to effect implicit truncation;
prefer an explicit `static_cast<>`.
With C++17, braced initializers work fine with `auto` too.
+Sometimes, however, there are better alternatives to empty braces;
+e.g., prefer `return std::nullopt;` to `return {};` to make it more clear
+that the function's result type is a `std::optional<>`.
1. Avoid unsigned types apart from `size_t`, which must be used with care.
When `int` just obviously works, just use `int`. When you need something
bigger than `int`, use `std::int64_t` rather than `long` or `long long`.
@@ -133,7 +136,8 @@ explicitly, it should contains either a `default:;` at its end or a
`default:` label that obviously crashes.
#### Classes
1. Define POD structures with `struct`.
-1. Don't use `this->` in (non-static) member functions.
+1. Don't use `this->` in (non-static) member functions, unless forced to
+do so in a template.
1. Define accessor and mutator member functions (implicitly) inline in the
class, after constructors and assignments. Don't needlessly define
(implicit) inline member functions in classes unless they really solve a
diff --git a/flang/lib/parser/basic-parsers.h b/flang/lib/parser/basic-parsers.h
index ec348fd518bc..c0f99f1cc85f 100644
--- a/flang/lib/parser/basic-parsers.h
+++ b/flang/lib/parser/basic-parsers.h
@@ -56,7 +56,7 @@ public:
constexpr explicit FailParser(MessageFixedText t) : text_{t} {}
std::optional<A> Parse(ParseState &state) const {
state.Say(text_);
- return {};
+ return std::nullopt;
}
private:
@@ -123,7 +123,7 @@ public:
ParseState forked{state};
forked.set_deferMessages(true);
if (parser_.Parse(forked)) {
- return {};
+ return std::nullopt;
}
return {Success{}};
}
@@ -149,7 +149,7 @@ public:
if (parser_.Parse(forked).has_value()) {
return {Success{}};
}
- return {};
+ return std::nullopt;
}
private:
@@ -491,7 +491,7 @@ public:
}
return {std::move(result)};
}
- return {};
+ return std::nullopt;
}
private:
@@ -626,7 +626,7 @@ public:
if (std::optional<paType> ax{parser_.Parse(state)}) {
return {function_(std::move(*ax))};
}
- return {};
+ return std::nullopt;
}
private:
@@ -653,7 +653,7 @@ public:
if (std::optional<paType> ax{parser_.Parse(state)}) {
return {functor_(std::move(*ax))};
}
- return {};
+ return std::nullopt;
}
private:
@@ -709,7 +709,7 @@ public:
return {function_(std::move(*ax), std::move(*bx))};
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -741,7 +741,7 @@ public:
return {function_(std::move(*ax), std::move(*bx))};
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -773,7 +773,7 @@ public:
return result;
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -807,7 +807,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -845,7 +845,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -886,7 +886,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -929,7 +929,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -971,7 +971,7 @@ template<class T, typename PA> struct Construct01 {
if (std::optional<Success>{parser_.Parse(state)}) {
return {T{}};
}
- return {};
+ return std::nullopt;
}
private:
@@ -987,7 +987,7 @@ public:
if (auto ax{parser_.Parse(state)}) {
return {T(std::move(*ax))};
}
- return {};
+ return std::nullopt;
}
private:
@@ -1023,7 +1023,7 @@ public:
return {T{std::move(*ax), std::move(*bx)}};
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -1050,7 +1050,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -1083,7 +1083,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -1121,7 +1121,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -1163,7 +1163,7 @@ public:
}
}
}
- return {};
+ return std::nullopt;
}
private:
@@ -1238,7 +1238,7 @@ template<bool pass> struct FixedParser {
if (pass) {
return {Success{}};
}
- return {};
+ return std::nullopt;
}
};
@@ -1256,7 +1256,7 @@ constexpr struct NextCh {
return result;
}
state.Say("end of file"_err_en_US);
- return {};
+ return std::nullopt;
}
} nextCh;
@@ -1271,7 +1271,7 @@ public:
std::optional<resultType> Parse(ParseState &state) const {
if (UserState * ustate{state.userState()}) {
if (!ustate->features().IsEnabled(LF)) {
- return {};
+ return std::nullopt;
}
}
auto at{state.GetLocation()};
@@ -1303,7 +1303,7 @@ public:
std::optional<resultType> Parse(ParseState &state) const {
if (UserState * ustate{state.userState()}) {
if (!ustate->features().IsEnabled(LF)) {
- return {};
+ return std::nullopt;
}
}
auto at{state.GetLocation()};
diff --git a/flang/lib/parser/characters.cc b/flang/lib/parser/characters.cc
index 6b6c2e97131c..e25106412992 100644
--- a/flang/lib/parser/characters.cc
+++ b/flang/lib/parser/characters.cc
@@ -36,7 +36,7 @@ std::optional<int> UTF8CharacterBytes(const char *p) {
return {2};
}
}
- return {};
+ return std::nullopt;
}
std::optional<int> EUC_JPCharacterBytes(const char *p) {
@@ -64,7 +64,7 @@ std::optional<int> EUC_JPCharacterBytes(const char *p) {
return {3};
}
}
- return {};
+ return std::nullopt;
}
std::optional<std::size_t> CountCharacters(
@@ -75,7 +75,7 @@ std::optional<std::size_t> CountCharacters(
++chars;
std::optional<int> cb{cbf(p)};
if (!cb.has_value()) {
- return {};
+ return std::nullopt;
}
p += *cb;
}
diff --git a/flang/lib/parser/characters.h b/flang/lib/parser/characters.h
index 02b96b2470dc..9a088cf25177 100644
--- a/flang/lib/parser/characters.h
+++ b/flang/lib/parser/characters.h
@@ -113,7 +113,7 @@ inline constexpr std::optional<char> BackslashEscapeValue(char ch) {
case '"':
case '\'':
case '\\': return {ch};
- default: return {};
+ default: return std::nullopt;
}
}
@@ -129,7 +129,7 @@ inline constexpr std::optional<char> BackslashEscapeChar(char ch) {
case '"':
case '\'':
case '\\': return {ch};
- default: return {};
+ default: return std::nullopt;
}
}
diff --git a/flang/lib/parser/instrumented-parser.h b/flang/lib/parser/instrumented-parser.h
index 71e9cbd4bc69..aa29293300c0 100644
--- a/flang/lib/parser/instrumented-parser.h
+++ b/flang/lib/parser/instrumented-parser.h
@@ -61,7 +61,7 @@ public:
if (ParsingLog * log{ustate->log()}) {
const char *at{state.GetLocation()};
if (log->Fails(at, tag_, state)) {
- return {};
+ return std::nullopt;
}
Messages messages{std::move(state.messages())};
std::optional<resultType> result{parser_.Parse(state)};
diff --git a/flang/lib/parser/parse-state.h b/flang/lib/parser/parse-state.h
index 86f1f630fce0..ab8b80a5fe41 100644
--- a/flang/lib/parser/parse-state.h
+++ b/flang/lib/parser/parse-state.h
@@ -197,14 +197,14 @@ public:
std::optional<const char *> GetNextChar() {
if (p_ >= limit_) {
- return {};
+ return std::nullopt;
}
return {UncheckedAdvance()};
}
std::optional<const char *> PeekAtNextChar() const {
if (p_ >= limit_) {
- return {};
+ return std::nullopt;
}
return {p_};
}
diff --git a/flang/lib/parser/preprocessor.cc b/flang/lib/parser/preprocessor.cc
index 9b26ae57c0df..159619118885 100644
--- a/flang/lib/parser/preprocessor.cc
+++ b/flang/lib/parser/preprocessor.cc
@@ -236,7 +236,7 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
}
}
if (j == tokens) {
- return {}; // input contains nothing that would be replaced
+ return std::nullopt; // input contains nothing that would be replaced
}
TokenSequence result{input, 0, j};
for (; j < tokens; ++j) {
diff --git a/flang/lib/parser/prescan.cc b/flang/lib/parser/prescan.cc
index 2f1440aafa9c..99f8955297bb 100644
--- a/flang/lib/parser/prescan.cc
+++ b/flang/lib/parser/prescan.cc
@@ -602,14 +602,14 @@ std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
const char *p{SkipWhiteSpace(start)};
for (char ch : "include"s) {
if (ToLowerCaseLetter(*p++) != ch) {
- return {};
+ return std::nullopt;
}
}
p = SkipWhiteSpace(p);
if (*p == '"' || *p == '\'') {
return {p - start};
}
- return {};
+ return std::nullopt;
}
void Prescanner::FortranInclude(const char *firstQuote) {
@@ -856,7 +856,7 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
const char *p{start};
char col1{*p++};
if (!IsFixedFormCommentChar(col1)) {
- return {};
+ return std::nullopt;
}
char sentinel[5], *sp{sentinel};
int column{2};
@@ -877,11 +877,11 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
++p;
} else {
// This is a Continuation line, not an initial directive line.
- return {};
+ return std::nullopt;
}
}
if (sp == sentinel) {
- return {};
+ return std::nullopt;
}
*sp = '\0';
if (const char *ss{IsCompilerDirectiveSentinel(sentinel)}) {
@@ -889,7 +889,7 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
return {LineClassification{
LineClassification::Kind::CompilerDirective, payloadOffset, ss}};
}
- return {};
+ return std::nullopt;
}
std::optional<Prescanner::LineClassification>
@@ -897,7 +897,7 @@ Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
char sentinel[8];
const char *p{SkipWhiteSpace(start)};
if (*p++ != '!') {
- return {};
+ return std::nullopt;
}
for (std::size_t j{0}; j + 1 < sizeof sentinel; ++p, ++j) {
if (*p == '\n') {
@@ -921,7 +921,7 @@ Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
}
sentinel[j] = ToLowerCaseLetter(*p);
}
- return {};
+ return std::nullopt;
}
Prescanner &Prescanner::AddCompilerDirectiveSentinel(const std::string &dir) {
diff --git a/flang/lib/parser/token-parsers.h b/flang/lib/parser/token-parsers.h
index cc791176ab27..00e152405a4e 100644
--- a/flang/lib/parser/token-parsers.h
+++ b/flang/lib/parser/token-parsers.h
@@ -52,7 +52,7 @@ public:
}
}
state.Say(MessageExpectedText{set_});
- return {};
+ return std::nullopt;
}
private:
@@ -141,14 +141,14 @@ public:
if (!at.has_value()) {
at = nextCh.Parse(state);
if (!at.has_value()) {
- return {};
+ return std::nullopt;
}
}
if (spaceSkipping) {
if (**at == ' ') {
at = nextCh.Parse(state);
if (!at.has_value()) {
- return {};
+ return std::nullopt;
}
} else if (mandatoryFreeFormSpace_) {
MissingSpace(state);
@@ -158,7 +158,7 @@ public:
at.reset();
} else {
state.Say(start, MessageExpectedText{str_, bytes_});
- return {};
+ return std::nullopt;
}
}
state.set_anyTokenMatched();
@@ -224,25 +224,25 @@ struct CharLiteralChar {
auto at{state.GetLocation()};
std::optional<const char *> och{nextCh.Parse(state)};
if (!och.has_value()) {
- return {};
+ return std::nullopt;
}
char ch{**och};
if (ch == '\n') {
state.Say(CharBlock{at, state.GetLocation()},
"unclosed character constant"_err_en_US);
- return {};
+ return std::nullopt;
}
if (ch != '\\') {
return {Result::Bare(ch)};
}
if (!(och = nextCh.Parse(state)).has_value()) {
- return {};
+ return std::nullopt;
}
ch = **och;
if (ch == '\n') {
state.Say(CharBlock{at, state.GetLocation()},
"unclosed character constant"_err_en_US);
- return {};
+ return std::nullopt;
}
if (std::optional<char> escChar{BackslashEscapeValue(ch)}) {
return {Result::Escaped(*escChar)};
@@ -270,7 +270,7 @@ struct CharLiteralChar {
ch = 16 * ch + HexadecimalDigitValue(**och);
}
} else {
- return {};
+ return std::nullopt;
}
} else {
state.Say(at, "bad escaped character"_en_US);
@@ -293,7 +293,7 @@ template<char quote> struct CharLiteral {
}
str += ch->ch;
}
- return {};
+ return std::nullopt;
}
};
@@ -318,30 +318,30 @@ struct BOZLiteral {
const char *start{state.GetLocation()};
std::optional<const char *> at{nextCh.Parse(state)};
if (!at.has_value()) {
- return {};
+ return std::nullopt;
}
if (**at == 'x' &&
!state.IsNonstandardOk(
LanguageFeature::BOZExtensions, "nonstandard BOZ literal"_en_US)) {
- return {};
+ return std::nullopt;
}
if (baseChar(**at)) {
at = nextCh.Parse(state);
if (!at.has_value()) {
- return {};
+ return std::nullopt;
}
}
char quote = **at;
if (quote != '\'' && quote != '"') {
- return {};
+ return std::nullopt;
}
std::string content;
while (true) {
at = nextCh.Parse(state);
if (!at.has_value()) {
- return {};
+ return std::nullopt;
}
if (**at == quote) {
break;
@@ -350,7 +350,7 @@ struct BOZLiteral {
continue;
}
if (!IsHexadecimalDigit(**at)) {
- return {};
+ return std::nullopt;
}
content += **at;
}
@@ -360,14 +360,14 @@ struct BOZLiteral {
if (!(at = nextCh.Parse(state)).has_value() || !baseChar(**at) ||
!state.IsNonstandardOk(LanguageFeature::BOZExtensions,
"nonstandard BOZ literal"_en_US)) {
- return {};
+ return std::nullopt;
}
spaceCheck.Parse(state);
}
if (content.empty()) {
state.Say(start, "no digit in BOZ literal"_err_en_US);
- return {};
+ return std::nullopt;
}
std::uint64_t value{0};
@@ -375,13 +375,13 @@ struct BOZLiteral {
digit = HexadecimalDigitValue(digit);
if ((digit >> *shift) > 0) {
state.Say(start, "bad digit in BOZ literal"_err_en_US);
- return {};
+ return std::nullopt;
}
std::uint64_t was{value};
value <<= *shift;
if ((value >> *shift) != was) {
state.Say(start, "excessive digits in BOZ literal"_err_en_US);
- return {};
+ return std::nullopt;
}
value |= digit;
}
@@ -396,7 +396,7 @@ constexpr struct DigitString {
static std::optional<std::uint64_t> Parse(ParseState &state) {
std::optional<const char *> firstDigit{digit.Parse(state)};
if (!firstDigit.has_value()) {
- return {};
+ return std::nullopt;
}
std::uint64_t value = **firstDigit - '0';
bool overflow{false};
@@ -434,7 +434,7 @@ constexpr struct SkipDigitString {
return {Success{}};
}
}
- return {};
+ return std::nullopt;
}
} skipDigitString;
@@ -471,7 +471,7 @@ struct SignedIntLiteralConstantWithoutKind {
if (minus.Parse(state)) {
negate = true;
} else if (!plus.Parse(state).has_value()) {
- return {};
+ return std::nullopt;
}
return SignedInteger(digitString.Parse(state), at, negate, state);
}
@@ -485,7 +485,7 @@ struct SignedDigitString {
static std::optional<std::int64_t> Parse(ParseState &state) {
std::optional<const char *> sign{state.PeekAtNextChar()};
if (!sign.has_value()) {
- return {};
+ return std::nullopt;
}
bool negate{**sign == '-'};
if (negate || **sign == '+') {
@@ -503,7 +503,7 @@ struct DigitStringIgnoreSpaces {
static constexpr auto getFirstDigit{space >> digit};
std::optional<const char *> firstDigit{getFirstDigit.Parse(state)};
if (!firstDigit.has_value()) {
- return {};
+ return std::nullopt;
}
std::uint64_t value = **firstDigit - '0';
bool overflow{false};
@@ -558,12 +558,12 @@ struct HollerithLiteral {
std::optional<std::uint64_t> charCount{
DigitStringIgnoreSpaces{}.Parse(state)};
if (!charCount.has_value() || *charCount < 1) {
- return {};
+ return std::nullopt;
}
static constexpr auto letterH{"h"_ch};
std::optional<const char *> h{letterH.Parse(state)};
if (!h.has_value()) {
- return {};
+ return std::nullopt;
}
std::string content;
for (auto j{*charCount}; j-- > 0;) {
@@ -573,14 +573,14 @@ struct HollerithLiteral {
std::optional<int> chBytes{EUC_JPCharacterBytes(p)};
if (!chBytes.has_value()) {
state.Say(start, "bad EUC_JP characters in Hollerith"_err_en_US);
- return {};
+ return std::nullopt;
}
bytes = *chBytes;
} else if (state.encoding() == Encoding::UTF8) {
std::optional<int> chBytes{UTF8CharacterBytes(p)};
if (!chBytes.has_value()) {
state.Say(start, "bad UTF-8 characters in Hollerith"_err_en_US);
- return {};
+ return std::nullopt;
}
bytes = *chBytes;
}
@@ -589,7 +589,7 @@ struct HollerithLiteral {
if (!at.has_value() || !isprint(**at)) {
state.Say(
start, "insufficient or bad characters in Hollerith"_err_en_US);
- return {};
+ return std::nullopt;
}
content += **at;
} else {
@@ -612,7 +612,7 @@ constexpr struct ConsumedAllInputParser {
if (state.IsAtEnd()) {
return {Success{}};
}
- return {};
+ return std::nullopt;
}
} consumedAllInput;
@@ -626,7 +626,7 @@ template<char goal> struct SkipPast {
return {Success{}};
}
}
- return {};
+ return std::nullopt;
}
};
@@ -641,7 +641,7 @@ template<char goal> struct SkipTo {
}
state.UncheckedAdvance();
}
- return {};
+ return std::nullopt;
}
};
diff --git a/flang/lib/parser/user-state.cc b/flang/lib/parser/user-state.cc
index fd9a6f9e4799..fb01ac8f5f8a 100644
--- a/flang/lib/parser/user-state.cc
+++ b/flang/lib/parser/user-state.cc
@@ -56,7 +56,7 @@ EndDoStmtForCapturedLabelDoStmt::Parse(ParseState &state) {
}
}
}
- return {};
+ return std::nullopt;
}
std::optional<Success> EnterNonlabelDoConstruct::Parse(ParseState &state) {
@@ -81,7 +81,7 @@ std::optional<Name> OldStructureComponentName::Parse(ParseState &state) {
}
}
}
- return {};
+ return std::nullopt;
}
std::optional<DataComponentDefStmt> StructureComponents::Parse(
diff --git a/flang/lib/semantics/expression.cc b/flang/lib/semantics/expression.cc
index e9f35fd86b78..7a74e017348d 100644
--- a/flang/lib/semantics/expression.cc
+++ b/flang/lib/semantics/expression.cc
@@ -32,7 +32,7 @@ std::optional<evaluate::GenericExpr> AnalyzeHelper(
if (result.has_value()) {
if (result->Rank() > 1) {
ea.context().messages.Say("must be scalar"_err_en_US);
- return {};
+ return std::nullopt;
}
}
return result;
@@ -46,7 +46,7 @@ std::optional<evaluate::GenericExpr> AnalyzeHelper(
result->Fold(ea.context());
if (!result->ScalarValue().has_value()) {
ea.context().messages.Say("must be constant"_err_en_US);
- return {};
+ return std::nullopt;
}
}
return result;
@@ -59,7 +59,7 @@ std::optional<evaluate::GenericExpr> AnalyzeHelper(
if (result.has_value() &&
!std::holds_alternative<evaluate::AnyKindIntegerExpr>(result->u)) {
ea.context().messages.Say("must be integer"_err_en_US);
- return {};
+ return std::nullopt;
}
return result;
}
@@ -68,7 +68,7 @@ template<>
std::optional<evaluate::GenericExpr> AnalyzeHelper(
ExpressionAnalyzer &ea, const parser::Name &n) {
// TODO
- return {};
+ return std::nullopt;
}
ExpressionAnalyzer::KindParam ExpressionAnalyzer::Analyze(
@@ -118,7 +118,7 @@ std::optional<evaluate::GenericExpr> AnalyzeHelper(
ea.context().messages.Say(parser::MessageFormattedText{
"unimplemented INTEGER kind (%ju)"_err_en_US,
static_cast<std::uintmax_t>(kind)});
- return {};
+ return std::nullopt;
}
}