使用sourcemonitor发现的一些问题

简介: 使用sourcemonitor发现的一些问题

使用sourcemonitor发现的一些问题

当代码中含中文注释时会影响计算结果:

(以下面代码片段为例)

情况一:

#include <iostream>
#include <vector>
using namespace std;
vector<int> arr;
int tmp = 0;
void fab(int n)
{
  if (n == 0) return 0;
  if (n <= 2) return 1;
  else return fab(n - 1) + fab(n - 2);
}
void fab1(int n)
{
  if (n == 0) return 0;
  if (n <= 2) return 1;
  else return fab(n - 1) + fab(n - 2);
}
void fab2(int n)
{
  if (n == 0) return 0;
  if (n <= 2) return 1;
  else return fab(n - 1) + fab(n - 2);
}
int binary_search(int tmp) {
  int l = 0, r = arr.size() - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (arr[mid] > tmp) {
      r = mid - 1;
    } else if (arr[mid] < tmp){
      l = mid + 1;
    } else {
      return mid;
    }
  }
  return -1;
}
int main() {
  for (int i = 0; i < 10; i++) {
    arr.push_back(i);
  }
  cout << binary_search(0);
  return 0;
}

这时不包含注释,计算结果为:

当在代码中试图加入一些 中文注释和英文注释

void fab(int n)
{
  //this is fab();
  if (n == 0) return 0;
  if (n <= 2) return 1;
  else return fab(n - 1) + fab(n - 2);
}
void fab1(int n)
{
  if (n == 0) return 0;//这是中文注释
  if (n <= 2) return 1;//这是中文注释
  else return fab(n - 1) + fab(n - 2);
}
//加在这
void fab2(int n)
{
  if (n == 0) return 0;
  if (n <= 2) return 1;
  else return fab(n - 1) + fab(n - 2);
}
int binary_search(int tmp) {
  //二分搜索
  int l = 0, r = arr.size() - 1;
  //当l要r小于等于时
  while (l <= r) {
    //这里可以优化一下
    int mid = (l + r) >> 1;
    if (arr[mid] > tmp) {
      r = mid - 1;
    } else if (arr[mid] < tmp){
      l = mid + 1;
    } else {
      return mid;
    }
  }
  return -1;
}

这时计算结果为:

这里加注释后binary_search函数的复杂度发生了变化,最为严重的是竟然记录不到fab2函数了。

这是因为fab2函数的头上有一条中文注释,sourcemonitor在进行解析时,解析成了:

(sourcemonitor生成的结果中,右击对应的文件,选择View Source File可以查看源文件)

函数名跑到了注释一行,自然就找不到fab2了。

情况二:

情况二也是中文引起的,但是这个我找了好久才发现这个bug

如果将上面代码段加上一行#define HONE ("定向的")

比如:

#include <iostream>
#include <vector>
using namespace std;
#define HONE ("定向的")
vector<int> arr;
int tmp = 0;
//(...这里和上面一样)
int main() {
  for (int i = 0; i < 10; i++) {
    arr.push_back(i);
  }
  cout << binary_search(0);
  return 0;
}

计算结果为:

可以发现结果为空。

查看一下源文件:

这是因为sourcemonitor在解析"定向的"这个中文字符串时,只得到了一个" ,因此它会一直找另一个"与它匹配,但我们代码中恰好没有",所以找到文件结尾都没匹配成功,自然也就六亲不认,一个函数都没计算。

为了验证上述结论,可以在下面再写一行宏,这样就有两个"了,匹配后就能计算后面的函数了。

可以发现果真如此(虽然此时结果仍然是错的,因为代码中含中文注释):

相关文章
|
5天前
Qt提升控件类为自定义类
Qt提升控件类为自定义类
|
5天前
|
小程序
Qt实现一个卡片橱窗
Qt实现一个卡片橱窗
|
5天前
|
Java API Android开发
DDS、FastDDS、OpenDDS扫盲
DDS、FastDDS、OpenDDS扫盲
296 0
|
5天前
|
API 调度
2.3.1 协程设计原理与汇编实现
2.3.1 协程设计原理与汇编实现
|
5天前
|
设计模式
设计模式-代理模式
设计模式-代理模式
|
5天前
|
Ubuntu
ubuntu安装protobuf
ubuntu安装protobuf
|
5天前
|
负载均衡 网络协议 小程序
Nginx配置Tcp负载均衡
Nginx配置Tcp负载均衡
|
5天前
|
程序员 C++
C++中第三方库的一般使用方式(libxl库为例)
C++中第三方库的一般使用方式(libxl库为例)
118 0
|
5天前
|
网络协议 虚拟化 网络架构
桥接模式和NAT模式的区别
桥接模式和NAT模式的区别
121 0
|
5天前
|
C语言
Centos7.6 gcc 4.8升级至7.3
Centos7.6 gcc 4.8升级至7.3