Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/Distro.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
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
#include "clang/Driver/Distro.h"
10
#include "clang/Basic/LLVM.h"
11
#include "llvm/ADT/SmallVector.h"
12
#include "llvm/ADT/StringRef.h"
13
#include "llvm/ADT/StringSwitch.h"
14
#include "llvm/Support/ErrorOr.h"
15
#include "llvm/Support/MemoryBuffer.h"
16
#include "llvm/ADT/Triple.h"
17
18
using namespace clang::driver;
19
using namespace clang;
20
21
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS,
22
42.7k
                                       const llvm::Triple &TargetOrHost) {
23
42.7k
  // If we don't target Linux, no need to check the distro. This saves a few
24
42.7k
  // OS calls.
25
42.7k
  if (!TargetOrHost.isOSLinux())
26
35.3k
    return Distro::UnknownDistro;
27
7.47k
28
7.47k
  // If the host is not running Linux, and we're backed by a real file system,
29
7.47k
  // no need to check the distro. This is the case where someone is
30
7.47k
  // cross-compiling from BSD or Windows to Linux, and it would be meaningless
31
7.47k
  // to try to figure out the "distro" of the non-Linux host.
32
7.47k
  IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
33
7.47k
      llvm::vfs::getRealFileSystem();
34
7.47k
  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
35
7.47k
  if (
!HostTriple.isOSLinux()7.47k
&& &VFS == RealFS.get())
36
7.44k
    return Distro::UnknownDistro;
37
32
38
32
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
39
32
      VFS.getBufferForFile("/etc/lsb-release");
40
32
  if (File) {
41
2
    StringRef Data = File.get()->getBuffer();
42
2
    SmallVector<StringRef, 16> Lines;
43
2
    Data.split(Lines, "\n");
44
2
    Distro::DistroType Version = Distro::UnknownDistro;
45
2
    for (StringRef Line : Lines)
46
10
      if (Version == Distro::UnknownDistro && 
Line.startswith("DISTRIB_CODENAME=")6
)
47
2
        Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
48
2
                      .Case("hardy", Distro::UbuntuHardy)
49
2
                      .Case("intrepid", Distro::UbuntuIntrepid)
50
2
                      .Case("jaunty", Distro::UbuntuJaunty)
51
2
                      .Case("karmic", Distro::UbuntuKarmic)
52
2
                      .Case("lucid", Distro::UbuntuLucid)
53
2
                      .Case("maverick", Distro::UbuntuMaverick)
54
2
                      .Case("natty", Distro::UbuntuNatty)
55
2
                      .Case("oneiric", Distro::UbuntuOneiric)
56
2
                      .Case("precise", Distro::UbuntuPrecise)
57
2
                      .Case("quantal", Distro::UbuntuQuantal)
58
2
                      .Case("raring", Distro::UbuntuRaring)
59
2
                      .Case("saucy", Distro::UbuntuSaucy)
60
2
                      .Case("trusty", Distro::UbuntuTrusty)
61
2
                      .Case("utopic", Distro::UbuntuUtopic)
62
2
                      .Case("vivid", Distro::UbuntuVivid)
63
2
                      .Case("wily", Distro::UbuntuWily)
64
2
                      .Case("xenial", Distro::UbuntuXenial)
65
2
                      .Case("yakkety", Distro::UbuntuYakkety)
66
2
                      .Case("zesty", Distro::UbuntuZesty)
67
2
                      .Case("artful", Distro::UbuntuArtful)
68
2
                      .Case("bionic", Distro::UbuntuBionic)
69
2
                      .Case("cosmic", Distro::UbuntuCosmic)
70
2
                      .Case("disco", Distro::UbuntuDisco)
71
2
                      .Case("eoan", Distro::UbuntuEoan)
72
2
                      .Case("focal", Distro::UbuntuFocal)
73
2
                      .Default(Distro::UnknownDistro);
74
2
    if (Version != Distro::UnknownDistro)
75
2
      return Version;
76
30
  }
77
30
78
30
  File = VFS.getBufferForFile("/etc/redhat-release");
79
30
  if (File) {
80
2
    StringRef Data = File.get()->getBuffer();
81
2
    if (Data.startswith("Fedora release"))
82
1
      return Distro::Fedora;
83
1
    if (Data.startswith("Red Hat Enterprise Linux") ||
84
1
        Data.startswith("CentOS") ||
85
1
        
Data.startswith("Scientific Linux")0
) {
86
1
      if (Data.find("release 7") != StringRef::npos)
87
1
        return Distro::RHEL7;
88
0
      else if (Data.find("release 6") != StringRef::npos)
89
0
        return Distro::RHEL6;
90
0
      else if (Data.find("release 5") != StringRef::npos)
91
0
        return Distro::RHEL5;
92
0
    }
93
0
    return Distro::UnknownDistro;
94
0
  }
95
28
96
28
  File = VFS.getBufferForFile("/etc/debian_version");
97
28
  if (File) {
98
2
    StringRef Data = File.get()->getBuffer();
99
2
    // Contents: < major.minor > or < codename/sid >
100
2
    int MajorVersion;
101
2
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
102
1
      switch (MajorVersion) {
103
0
      case 5:
104
0
        return Distro::DebianLenny;
105
0
      case 6:
106
0
        return Distro::DebianSqueeze;
107
0
      case 7:
108
0
        return Distro::DebianWheezy;
109
1
      case 8:
110
1
        return Distro::DebianJessie;
111
0
      case 9:
112
0
        return Distro::DebianStretch;
113
0
      case 10:
114
0
        return Distro::DebianBuster;
115
0
      case 11:
116
0
        return Distro::DebianBullseye;
117
0
      default:
118
0
        return Distro::UnknownDistro;
119
1
      }
120
1
    }
121
1
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
122
1
        .Case("squeeze/sid", Distro::DebianSqueeze)
123
1
        .Case("wheezy/sid", Distro::DebianWheezy)
124
1
        .Case("jessie/sid", Distro::DebianJessie)
125
1
        .Case("stretch/sid", Distro::DebianStretch)
126
1
        .Case("buster/sid", Distro::DebianBuster)
127
1
        .Case("bullseye/sid", Distro::DebianBullseye)
128
1
        .Default(Distro::UnknownDistro);
129
1
  }
130
26
131
26
  File = VFS.getBufferForFile("/etc/SuSE-release");
132
26
  if (File) {
133
3
    StringRef Data = File.get()->getBuffer();
134
3
    SmallVector<StringRef, 8> Lines;
135
3
    Data.split(Lines, "\n");
136
6
    for (const StringRef& Line : Lines) {
137
6
      if (!Line.trim().startswith("VERSION"))
138
3
        continue;
139
3
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
140
3
      // Old versions have split VERSION and PATCHLEVEL
141
3
      // Newer versions use VERSION = x.y
142
3
      std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
143
3
      int Version;
144
3
145
3
      // OpenSUSE/SLES 10 and older are not supported and not compatible
146
3
      // with our rules, so just treat them as Distro::UnknownDistro.
147
3
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
148
2
        return Distro::OpenSUSE;
149
1
      return Distro::UnknownDistro;
150
1
    }
151
3
    
return Distro::UnknownDistro0
;
152
23
  }
153
23
154
23
  if (VFS.exists("/etc/exherbo-release"))
155
1
    return Distro::Exherbo;
156
22
157
22
  if (VFS.exists("/etc/alpine-release"))
158
0
    return Distro::AlpineLinux;
159
22
160
22
  if (VFS.exists("/etc/arch-release"))
161
1
    return Distro::ArchLinux;
162
21
163
21
  if (VFS.exists("/etc/gentoo-release"))
164
1
    return Distro::Gentoo;
165
20
166
20
  return Distro::UnknownDistro;
167
20
}
168
169
Distro::Distro(llvm::vfs::FileSystem &VFS, const llvm::Triple &TargetOrHost)
170
42.7k
    : DistroVal(DetectDistro(VFS, TargetOrHost)) {}