Skip to content

Commit 6a8f733

Browse files
committed
refactor Index.cpp
1 parent 38de43d commit 6a8f733

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

source/lib/Index.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#include "Index.hpp"
13+
#include "Reduce.h"
14+
#include "Representation.h"
15+
#include "Namespace.hpp"
16+
//#include <mrdox/Config.hpp>
17+
//#include <llvm/Support/Error.h>
18+
//#include <llvm/Support/Path.h>
19+
20+
namespace clang {
21+
namespace mrdox {
22+
23+
// Dispatch function.
24+
llvm::Expected<std::unique_ptr<Info>>
25+
mergeInfos(std::vector<std::unique_ptr<Info>>& Values)
26+
{
27+
if (Values.empty() || !Values[0])
28+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
29+
"no info values to merge");
30+
31+
switch (Values[0]->IT) {
32+
case InfoType::IT_namespace:
33+
return reduce<NamespaceInfo>(Values);
34+
case InfoType::IT_record:
35+
return reduce<RecordInfo>(Values);
36+
case InfoType::IT_enum:
37+
return reduce<EnumInfo>(Values);
38+
case InfoType::IT_function:
39+
return reduce<FunctionInfo>(Values);
40+
case InfoType::IT_typedef:
41+
return reduce<TypedefInfo>(Values);
42+
default:
43+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
44+
"unexpected info type");
45+
}
46+
}
47+
48+
// Order is based on the Name attribute:
49+
// case insensitive order
50+
bool
51+
Index::
52+
operator<(
53+
Index const& Other) const
54+
{
55+
// Loop through each character of both strings
56+
for (unsigned I = 0; I < Name.size() && I < Other.Name.size(); ++I) {
57+
// Compare them after converting both to lower case
58+
int D = tolower(Name[I]) - tolower(Other.Name[I]);
59+
if (D == 0)
60+
continue;
61+
return D < 0;
62+
}
63+
// If both strings have the size it means they would be equal if changed to
64+
// lower case. In here, lower case will be smaller than upper case
65+
// Example: string < stRing = true
66+
// This is the opposite of how operator < handles strings
67+
if (Name.size() == Other.Name.size())
68+
return Name > Other.Name;
69+
// If they are not the same size; the shorter string is smaller
70+
return Name.size() < Other.Name.size();
71+
}
72+
73+
void Index::sort() {
74+
llvm::sort(Children);
75+
for (auto& C : Children)
76+
C.sort();
77+
}
78+
79+
} // mrdox
80+
} // clang

source/lib/Representation.cpp

-59
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,15 @@
2323
// on members on the forward declaration, but would have the class name).
2424
//
2525

26-
#include "Index.hpp"
27-
#include "Reduce.h"
2826
#include "Representation.h"
2927
#include "Namespace.hpp"
3028
#include <mrdox/Config.hpp>
3129
#include "llvm/Support/Error.h"
3230
#include "llvm/Support/Path.h"
3331

34-
static_assert(clang::AccessSpecifier::AS_public == 0);
35-
static_assert(clang::AccessSpecifier::AS_protected == 1);
36-
static_assert(clang::AccessSpecifier::AS_private == 2);
37-
static_assert(clang::AccessSpecifier::AS_none == 3);
38-
3932
namespace clang {
4033
namespace mrdox {
4134

42-
// Dispatch function.
43-
llvm::Expected<std::unique_ptr<Info>>
44-
mergeInfos(std::vector<std::unique_ptr<Info>>& Values) {
45-
if (Values.empty() || !Values[0])
46-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
47-
"no info values to merge");
48-
49-
switch (Values[0]->IT) {
50-
case InfoType::IT_namespace:
51-
return reduce<NamespaceInfo>(Values);
52-
case InfoType::IT_record:
53-
return reduce<RecordInfo>(Values);
54-
case InfoType::IT_enum:
55-
return reduce<EnumInfo>(Values);
56-
case InfoType::IT_function:
57-
return reduce<FunctionInfo>(Values);
58-
case InfoType::IT_typedef:
59-
return reduce<TypedefInfo>(Values);
60-
default:
61-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
62-
"unexpected info type");
63-
}
64-
}
65-
6635
bool CommentInfo::operator==(const CommentInfo& Other) const {
6736
auto FirstCI = std::tie(Kind, Text, Name, Direction, ParamName, CloseName,
6837
SelfClosing, Explicit, AttrKeys, AttrValues, Args);
@@ -141,33 +110,5 @@ BaseRecordInfo::BaseRecordInfo(SymbolID USR, StringRef Name, StringRef Path,
141110
: RecordInfo(USR, Name, Path), IsVirtual(IsVirtual), Access(Access),
142111
IsParent(IsParent) {}
143112

144-
// Order is based on the Name attribute:
145-
// case insensitive order
146-
bool
147-
Index::operator<(const Index& Other) const {
148-
// Loop through each character of both strings
149-
for (unsigned I = 0; I < Name.size() && I < Other.Name.size(); ++I) {
150-
// Compare them after converting both to lower case
151-
int D = tolower(Name[I]) - tolower(Other.Name[I]);
152-
if (D == 0)
153-
continue;
154-
return D < 0;
155-
}
156-
// If both strings have the size it means they would be equal if changed to
157-
// lower case. In here, lower case will be smaller than upper case
158-
// Example: string < stRing = true
159-
// This is the opposite of how operator < handles strings
160-
if (Name.size() == Other.Name.size())
161-
return Name > Other.Name;
162-
// If they are not the same size; the shorter string is smaller
163-
return Name.size() < Other.Name.size();
164-
}
165-
166-
void Index::sort() {
167-
llvm::sort(Children);
168-
for (auto& C : Children)
169-
C.sort();
170-
}
171-
172113
} // mrdox
173114
} // clang

0 commit comments

Comments
 (0)