| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 
 | //===--- IndexTests.cpp - Test indexing actions -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Index/IndexDataConsumer.h"
#include "clang/Index/IndexSymbol.h"
#include "clang/Index/IndexingAction.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>
namespace clang {
namespace index {
namespace {
struct Position {
  size_t Line = 0;
  size_t Column = 0;
  Position(size_t Line = 0, size_t Column = 0) : Line(Line), Column(Column) {}
  static Position fromSourceLocation(SourceLocation Loc,
                                     const SourceManager &SM) {
    FileID FID;
    unsigned Offset;
    std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
    Position P;
    P.Line = SM.getLineNumber(FID, Offset);
    P.Column = SM.getColumnNumber(FID, Offset);
    return P;
  }
};
bool operator==(const Position &LHS, const Position &RHS) {
  return std::tie(LHS.Line, LHS.Column) == std::tie(RHS.Line, RHS.Column);
}
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &Pos) {
  return OS << Pos.Line << ':' << Pos.Column;
}
struct TestSymbol {
  std::string QName;
  Position WrittenPos;
  Position DeclPos;
  SymbolInfo SymInfo;
  SymbolRoleSet Roles;
  // FIXME: add more information.
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) {
  return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos << '('
            << static_cast<unsigned>(S.SymInfo.Kind) << ')';
}
class Indexer : public IndexDataConsumer {
public:
  void initialize(ASTContext &Ctx) override {
    AST = &Ctx;
    IndexDataConsumer::initialize(Ctx);
  }
  bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
                           ArrayRef<SymbolRelation>, SourceLocation Loc,
                           ASTNodeInfo) override {
    const auto *ND = llvm::dyn_cast<NamedDecl>(D);
    if (!ND)
      return true;
    TestSymbol S;
    S.SymInfo = getSymbolInfo(D);
    S.QName = ND->getQualifiedNameAsString();
    S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager());
    S.DeclPos =
        Position::fromSourceLocation(D->getLocation(), AST->getSourceManager());
    S.Roles = Roles;
    Symbols.push_back(std::move(S));
    return true;
  }
  bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI,
                            SymbolRoleSet Roles, SourceLocation Loc) override {
    TestSymbol S;
    S.SymInfo = getSymbolInfoForMacro(*MI);
    S.QName = Name->getName();
    S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager());
    S.DeclPos = Position::fromSourceLocation(MI->getDefinitionLoc(),
                                             AST->getSourceManager());
    S.Roles = Roles;
    Symbols.push_back(std::move(S));
    return true;
  }
  std::vector<TestSymbol> Symbols;
  const ASTContext *AST = nullptr;
};
class IndexAction : public ASTFrontendAction {
public:
  IndexAction(std::shared_ptr<Indexer> Index,
              IndexingOptions Opts = IndexingOptions())
      : Index(std::move(Index)), Opts(Opts) {}
protected:
  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                 StringRef InFile) override {
    class Consumer : public ASTConsumer {
      std::shared_ptr<Indexer> Index;
      std::shared_ptr<Preprocessor> PP;
      IndexingOptions Opts;
    public:
      Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP,
               IndexingOptions Opts)
          : Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {}
      void HandleTranslationUnit(ASTContext &Ctx) override {
        std::vector<Decl *> DeclsToIndex(
            Ctx.getTranslationUnitDecl()->decls().begin(),
            Ctx.getTranslationUnitDecl()->decls().end());
        indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts);
      }
    };
    return llvm::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts);
  }
private:
  std::shared_ptr<Indexer> Index;
  IndexingOptions Opts;
};
using testing::AllOf;
using testing::Contains;
using testing::Not;
using testing::UnorderedElementsAre;
MATCHER_P(QName, Name, "") { return arg.QName == Name; }
MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; }
MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; }
MATCHER_P(Kind, SymKind, "") { return arg.SymInfo.Kind == SymKind; }
MATCHER_P(HasRole, Role, "") { return arg.Roles & static_cast<unsigned>(Role); }
TEST(IndexTest, Simple) {
  auto Index = std::make_shared<Indexer>();
  tooling::runToolOnCode(new IndexAction(Index), "class X {}; void f() {}");
  EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f")));
}
TEST(IndexTest, IndexPreprocessorMacros) {
  std::string Code = "#define INDEX_MAC 1";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  Opts.IndexMacrosInPreprocessor = true;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols, Contains(QName("INDEX_MAC")));
  Opts.IndexMacrosInPreprocessor = false;
  Index->Symbols.clear();
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols, UnorderedElementsAre());
}
TEST(IndexTest, IndexParametersInDecls) {
  std::string Code = "void foo(int bar);";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  Opts.IndexFunctionLocals = true;
  Opts.IndexParametersInDeclarations = true;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols, Contains(QName("bar")));
  Opts.IndexParametersInDeclarations = false;
  Index->Symbols.clear();
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
}
TEST(IndexTest, IndexExplicitTemplateInstantiation) {
  std::string Code = R"cpp(
    template <typename T>
    struct Foo { void bar() {} };
    template <>
    struct Foo<int> { void bar() {} };
    void foo() {
      Foo<char> abc;
      Foo<int> b;
    }
  )cpp";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols,
              AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
                                   DeclAt(Position(5, 12)))),
                    Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)),
                                   DeclAt(Position(3, 12))))));
}
TEST(IndexTest, IndexTemplateInstantiationPartial) {
  std::string Code = R"cpp(
    template <typename T1, typename T2>
    struct Foo { void bar() {} };
    template <typename T>
    struct Foo<T, int> { void bar() {} };
    void foo() {
      Foo<char, char> abc;
      Foo<int, int> b;
    }
  )cpp";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols,
              Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
                             DeclAt(Position(5, 12)))));
}
TEST(IndexTest, IndexTypeParmDecls) {
  std::string Code = R"cpp(
    template <typename T, int I, template<typename> class C, typename NoRef>
    struct Foo {
      T t = I;
      C<int> x;
    };
  )cpp";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))),
                                    Not(Contains(QName("Foo::I"))),
                                    Not(Contains(QName("Foo::C"))),
                                    Not(Contains(QName("Foo::NoRef")))));
  Opts.IndexTemplateParameters = true;
  Index->Symbols.clear();
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols,
              AllOf(Contains(QName("Foo::T")), Contains(QName("Foo::I")),
                    Contains(QName("Foo::C")), Contains(QName("Foo::NoRef"))));
}
TEST(IndexTest, UsingDecls) {
  std::string Code = R"cpp(
    void foo(int bar);
    namespace std {
      using ::foo;
    }
  )cpp";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(Index->Symbols,
              Contains(AllOf(QName("std::foo"), Kind(SymbolKind::Using))));
}
TEST(IndexTest, Constructors) {
  std::string Code = R"cpp(
    struct Foo {
      Foo(int);
      ~Foo();
    };
  )cpp";
  auto Index = std::make_shared<Indexer>();
  IndexingOptions Opts;
  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
  EXPECT_THAT(
      Index->Symbols,
      UnorderedElementsAre(
          AllOf(QName("Foo"), Kind(SymbolKind::Struct),
                WrittenAt(Position(2, 12))),
          AllOf(QName("Foo::Foo"), Kind(SymbolKind::Constructor),
                WrittenAt(Position(3, 7))),
          AllOf(QName("Foo"), Kind(SymbolKind::Struct),
                HasRole(SymbolRole::NameReference), WrittenAt(Position(3, 7))),
          AllOf(QName("Foo::~Foo"), Kind(SymbolKind::Destructor),
                WrittenAt(Position(4, 7))),
          AllOf(QName("Foo"), Kind(SymbolKind::Struct),
                HasRole(SymbolRole::NameReference),
                WrittenAt(Position(4, 8)))));
}
} // namespace
} // namespace index
} // namespace clang
 |