[PATCH] D33187: [ELF] - Use to_integer instead of getAsInteger().

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 07:46:35 PDT 2017


LGTM

George Rimar via Phabricator <reviews at reviews.llvm.org> writes:

> grimar created this revision.
>
> llvm::to_integer appeared in r303011.
> I suggest to use to_integer everywhere in LLD instead getAsInteger() because
> API of latter is confusing. It returns true on error and false otherwise what makes
> reading the code incomfortable.
>
>
> https://reviews.llvm.org/D33187
>
> Files:
>   ELF/Driver.cpp
>   ELF/ScriptParser.cpp
>   ELF/Strings.cpp
>   ELF/Writer.cpp
>
> Index: ELF/Writer.cpp
> ===================================================================
> --- ELF/Writer.cpp
> +++ ELF/Writer.cpp
> @@ -1590,7 +1590,7 @@
>    if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
>      return B->getVA();
>    uint64_t Addr;
> -  if (!Config->Entry.getAsInteger(0, Addr))
> +  if (to_integer(Config->Entry, Addr))
>      return Addr;
>  
>    // Case 4
> Index: ELF/Strings.cpp
> ===================================================================
> --- ELF/Strings.cpp
> +++ ELF/Strings.cpp
> @@ -46,7 +46,7 @@
>    if (Pos == StringRef::npos)
>      return 65536;
>    int V;
> -  if (S.substr(Pos + 1).getAsInteger(10, V))
> +  if (!to_integer(S.substr(Pos + 1), V, 10))
>      return 65536;
>    return V;
>  }
> @@ -68,7 +68,7 @@
>      StringRef B = S.substr(0, 2);
>      S = S.substr(2);
>      uint8_t H;
> -    if (B.getAsInteger(16, H)) {
> +    if (!to_integer(B, H, 16)) {
>        error("not a hexadecimal value: " + B);
>        return {};
>      }
> Index: ELF/ScriptParser.cpp
> ===================================================================
> --- ELF/ScriptParser.cpp
> +++ ELF/ScriptParser.cpp
> @@ -639,7 +639,7 @@
>  // We are compatible with ld.gold because it's easier to implement.
>  uint32_t ScriptParser::parseFill(StringRef Tok) {
>    uint32_t V = 0;
> -  if (Tok.getAsInteger(0, V))
> +  if (!to_integer(Tok, V))
>      setError("invalid filler expression: " + Tok);
>  
>    uint32_t Buf;
> @@ -778,23 +778,23 @@
>  
>    // Hexadecimal
>    uint64_t Val;
> -  if (Tok.startswith_lower("0x") && !Tok.substr(2).getAsInteger(16, Val))
> +  if (Tok.startswith_lower("0x") && to_integer(Tok.substr(2), Val, 16))
>      return Val;
> -  if (Tok.endswith_lower("H") && !Tok.drop_back().getAsInteger(16, Val))
> +  if (Tok.endswith_lower("H") && to_integer(Tok.drop_back(), Val, 16))
>      return Val;
>  
>    // Decimal
>    if (Tok.endswith_lower("K")) {
> -    if (Tok.drop_back().getAsInteger(10, Val))
> +    if (!to_integer(Tok.drop_back(), Val, 10))
>        return None;
>      return Val * 1024;
>    }
>    if (Tok.endswith_lower("M")) {
> -    if (Tok.drop_back().getAsInteger(10, Val))
> +    if (!to_integer(Tok.drop_back(), Val, 10))
>        return None;
>      return Val * 1024 * 1024;
>    }
> -  if (Tok.getAsInteger(10, Val))
> +  if (!to_integer(Tok, Val, 10))
>      return None;
>    return Val;
>  }
> Index: ELF/Driver.cpp
> ===================================================================
> --- ELF/Driver.cpp
> +++ ELF/Driver.cpp
> @@ -284,7 +284,7 @@
>    int V = Default;
>    if (auto *Arg = Args.getLastArg(Key)) {
>      StringRef S = Arg->getValue();
> -    if (S.getAsInteger(10, V))
> +    if (!to_integer(S, V, 10))
>        error(Arg->getSpelling() + ": number expected, but got " + S);
>    }
>    return V;
> @@ -311,7 +311,7 @@
>      if (Pos != StringRef::npos && Key == Value.substr(0, Pos)) {
>        Value = Value.substr(Pos + 1);
>        uint64_t Result;
> -      if (Value.getAsInteger(0, Result))
> +      if (!to_integer(Value, Result))
>          error("invalid " + Key + ": " + Value);
>        return Result;
>      }
> @@ -522,7 +522,7 @@
>    uint64_t VA = 0;
>    if (S.startswith("0x"))
>      S = S.drop_front(2);
> -  if (S.getAsInteger(16, VA))
> +  if (!to_integer(S, VA, 16))
>      error("invalid argument: " + toString(Arg));
>    return VA;
>  }
> @@ -886,7 +886,7 @@
>  
>    StringRef S = Arg->getValue();
>    uint64_t V;
> -  if (S.getAsInteger(0, V)) {
> +  if (!to_integer(S, V)) {
>      error("-image-base: number expected, but got " + S);
>      return 0;
>    }


More information about the llvm-commits mailing list