[llvm] r241484 - Reduce code duplication. NFC.

Benjamin Kramer benny.kra at gmail.com
Mon Jul 6 12:12:19 PDT 2015


> On 06.07.2015, at 20:48, Rafael Espindola <rafael.espindola at gmail.com> wrote:
> 
> Author: rafael
> Date: Mon Jul  6 13:48:47 2015
> New Revision: 241484
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=241484&view=rev
> Log:
> Reduce code duplication. NFC.
> 
> Modified:
>    llvm/trunk/tools/llvm-nm/llvm-nm.cpp
> 
> Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=241484&r1=241483&r2=241484&view=diff
> ==============================================================================
> --- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
> +++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Mon Jul  6 13:48:47 2015
> @@ -185,60 +185,31 @@ struct NMSymbol {
> }
> 
> static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
> -  if (!ReverseSort) {
> -    if (A.Address < B.Address)
> -      return true;
> -    if (A.Address == B.Address && A.Name < B.Name)
> -      return true;
> -    if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
> -      return true;
> -    return false;
> -  }
> -
> -  if (A.Address > B.Address)
> +  if (A.Address < B.Address)
>     return true;
> -  if (A.Address == B.Address && A.Name > B.Name)
> +  if (A.Address == B.Address && A.Name < B.Name)
>     return true;
> -  if (A.Address == B.Address && A.Name == B.Name && A.Size > B.Size)
> +  if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
>     return true;
>   return false;
> }
> 
> static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
> -  if (!ReverseSort) {
> -    if (A.Size < B.Size)
> -      return true;
> -    if (A.Size == B.Size && A.Name < B.Name)
> -      return true;
> -    if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
> -      return true;
> -    return false;
> -  }
> -
> -  if (A.Size > B.Size)
> +  if (A.Size < B.Size)
>     return true;
> -  if (A.Size == B.Size && A.Name > B.Name)
> +  if (A.Size == B.Size && A.Name < B.Name)
>     return true;
> -  if (A.Size == B.Size && A.Name == B.Name && A.Address > B.Address)
> +  if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
>     return true;
>   return false;
> }
> 
> static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
> -  if (!ReverseSort) {
> -    if (A.Name < B.Name)
> -      return true;
> -    if (A.Name == B.Name && A.Size < B.Size)
> -      return true;
> -    if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
> -      return true;
> -    return false;
> -  }
> -  if (A.Name > B.Name)
> +  if (A.Name < B.Name)
>     return true;
> -  if (A.Name == B.Name && A.Size > B.Size)
> +  if (A.Name == B.Name && A.Size < B.Size)
>     return true;
> -  if (A.Name == B.Name && A.Size == B.Size && A.Address > B.Address)
> +  if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
>     return true;
>   return false;
> }
> @@ -526,12 +497,17 @@ static void sortAndPrintSymbolList(Symbo
>                                    std::string ArchiveName,
>                                    std::string ArchitectureName) {
>   if (!NoSort) {
> +    std::function<bool(const NMSymbol &, const NMSymbol &)> Cmp;
>     if (NumericSort)
> -      std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
> +      Cmp = compareSymbolAddress;
>     else if (SizeSort)
> -      std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
> +      Cmp = compareSymbolSize;
>     else
> -      std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
> +      Cmp = compareSymbolName;
> +
> +    if (ReverseSort)
> +      Cmp = [=](const NMSymbol &A, const NMSymbol &B) { return !Cmp(A, B); };

Won't this return true for A == B, violating the strict weak ordering? I think you can just swap A and B here to get the desired effect.

- Ben

> +    std::sort(SymbolList.begin(), SymbolList.end(), Cmp);
>   }
> 
>   if (!PrintFileName) {
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list