Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CIndexInclusionStack.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
2
//
3
// Part of the LLVM Project, 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
//===----------------------------------------------------------------------===//
8
//
9
// This file defines a callback mechanism for clients to get the inclusion
10
// stack from a translation unit.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "CIndexer.h"
15
#include "CXSourceLocation.h"
16
#include "CXTranslationUnit.h"
17
#include "clang/AST/DeclVisitor.h"
18
#include "clang/Frontend/ASTUnit.h"
19
using namespace clang;
20
21
static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n,
22
                          CXTranslationUnit TU, CXInclusionVisitor CB,
23
                          CXClientData clientData)
24
3
{
25
3
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
26
3
  SourceManager &SM = CXXUnit->getSourceManager();
27
3
  ASTContext &Ctx = CXXUnit->getASTContext();
28
3
  SmallVector<CXSourceLocation, 10> InclusionStack;
29
3
  const bool HasPreamble = SM.getPreambleFileID().isValid();
30
3
31
15
  for (unsigned i = 0 ; i < n ; 
++i12
) {
32
12
    bool Invalid = false;
33
12
    const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
34
12
35
12
    if (!SL.isFile() || 
Invalid10
)
36
2
      continue;
37
10
38
10
    const SrcMgr::FileInfo &FI = SL.getFile();
39
10
    if (!FI.getContentCache()->OrigEntry)
40
3
      continue;
41
7
42
7
    // If this is the main file, and there is a preamble, skip this SLoc. The
43
7
    // inclusions of the preamble already showed it.
44
7
    SourceLocation L = FI.getIncludeLoc();
45
7
    if (HasPreamble && 
CXXUnit->isInMainFileID(L)4
)
46
1
      continue;
47
6
48
6
    // Build the inclusion stack.
49
6
    InclusionStack.clear();
50
14
    while (L.isValid()) {
51
8
      PresumedLoc PLoc = SM.getPresumedLoc(L);
52
8
      InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
53
8
      L = PLoc.isValid()? PLoc.getIncludeLoc() : 
SourceLocation()0
;
54
8
    }
55
6
56
6
    // If there is a preamble, the last entry is the "inclusion" of that
57
6
    // preamble into the main file, which has the bogus entry of main.c:1:1
58
6
    if (HasPreamble && 
!InclusionStack.empty()3
)
59
2
      InclusionStack.pop_back();
60
6
61
6
    // Callback to the client.
62
6
    // FIXME: We should have a function to construct CXFiles.
63
6
    CB(static_cast<CXFile>(
64
6
         const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
65
6
       InclusionStack.data(), InclusionStack.size(), clientData);
66
6
  }
67
3
}
68
69
70
void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
71
2
                         CXClientData clientData) {
72
2
  if (cxtu::isNotUsableTU(TU)) {
73
0
    LOG_BAD_TU(TU);
74
0
    return;
75
0
  }
76
2
77
2
  SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
78
2
  const unsigned n =  SM.local_sloc_entry_size();
79
2
80
2
  // In the case where all the SLocEntries are in an external source, traverse
81
2
  // those SLocEntries as well.  This is the case where we are looking
82
2
  // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
83
2
  // a AST/PCH file, but this file has a pre-compiled preamble, we also need
84
2
  // to look in that file.
85
2
  if (n == 1 || SM.getPreambleFileID().isValid()) {
86
1
    getInclusions(&SourceManager::getLoadedSLocEntry,
87
1
                  SM.loaded_sloc_entry_size(), TU, CB, clientData);
88
1
  }
89
2
90
2
  // Not a PCH/AST file. Note, if there is a preamble, it could still be that
91
2
  // there are #includes in this file (e.g. for any include after the first
92
2
  // declaration).
93
2
  if (n != 1)
94
2
    getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData);
95
2
96
2
}